Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Type aliases don't work as object indexes - crash bug #152

Closed
nickretallack opened this issue Mar 3, 2020 · 7 comments
Closed

Type aliases don't work as object indexes - crash bug #152

nickretallack opened this issue Mar 3, 2020 · 7 comments
Assignees
Labels
bug Something isn't working conversion

Comments

@nickretallack
Copy link

If I make a file with only this in it:

type MyString = string;
type MyObject = {[id: MyString]: string};

And run the tool on it, this happens:

$ flow-to-ts myfile.js
error processing myfile.js
TypeError: Property name expected type of string but got null
    at Object.validate (/home/nretallack/.config/yarn/global/node_modules/@babel/types/lib/definitions/utils.js:159:13)
    at validateField (/home/nretallack/.config/yarn/global/node_modules/@babel/types/lib/validators/validate.js:24:9)
    at validate (/home/nretallack/.config/yarn/global/node_modules/@babel/types/lib/validators/validate.js:17:3)
    at builder (/home/nretallack/.config/yarn/global/node_modules/@babel/types/lib/builders/builder.js:38:27)
    at Object.TSTypeParameter (/home/nretallack/.config/yarn/global/node_modules/@babel/types/lib/builders/generated/index.js:1182:31)
    at indexers.forEach.indexer (/home/nretallack/.config/yarn/global/node_modules/@khanacademy/flow-to-ts/src/transform.js:564:35)
    at Array.forEach (<anonymous>)
    at Object.exit (/home/nretallack/.config/yarn/global/node_modules/@khanacademy/flow-to-ts/src/transform.js:554:16)
    at NodePath._call (/home/nretallack/.config/yarn/global/node_modules/@khanacademy/flow-to-ts/babel-traverse/lib/path/context.js:55:20)
    at NodePath.call (/home/nretallack/.config/yarn/global/node_modules/@khanacademy/flow-to-ts/babel-traverse/lib/path/context.js:42:17)

I suppose #108 didn't fix this.

@kevinbarabash kevinbarabash added bug Something isn't working conversion labels Mar 3, 2020
@nickretallack
Copy link
Author

It would be an improvement if it just assumed the type is always string, because object keys are always cast to string anyway, right?

@kevinbarabash
Copy link
Contributor

I think we should try to convert the types as closely as possible. Changing a type alias to string seems like it'd be a pretty big change even if it is being used as a key.

@nickretallack
Copy link
Author

Ultimately I did my typescript conversion using flowts. This guy maintains his own fork of babel-plugin-flow-to-typescript which is more up-to-date with bug fixes and support for new typescript 3.8 syntax. Also, flowts uses recast to avoid making unnecessary changes to your source code such as removing newlines and dropping comments.

@palexs
Copy link

palexs commented Jul 6, 2020

This is still an issue when running flow-to-ts tool. However, when I use https://flow-to-ts.netlify.app/ it does the transformation correctly.

@jdelStrother
Copy link

This looks like it was introduced sometime around babel 7.6. If I add the following to my package.json:

  "resolutions": {
    "**/flow-to-ts/@babel/types": "< 7.6.0"
  }

flow-to-ts is able to convert indexer properties like this -

input:

// @flow
type KK = ("foo"|"bar")
export type X = { [k: KK]: string}

output:

type KK = "foo" | "bar";
export type X = { [k in KK]?: string };

@kevinbarabash
Copy link
Contributor

@jdelStrother interesting. I've been meaning to update some of the dependencies. I'm glad to see doing so fixes some issues.

@jdelStrother
Copy link

Sorry, I wasn't clear. This used to work fine, but broke in Babel 7.6 or above 😞

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working conversion
Projects
None yet
Development

No branches or pull requests

4 participants