Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typeof frozen object property is generic, not literal type #7329

Open
philippelesaux opened this issue Jan 9, 2019 · 0 comments · May be fixed by #7962
Open

typeof frozen object property is generic, not literal type #7329

philippelesaux opened this issue Jan 9, 2019 · 0 comments · May be fixed by #7962

Comments

@philippelesaux
Copy link

In using frozen objects to create enums, it seems the literal value is not inferred to the type of that object's property.

In the following example, when using the $Values utility we get a union of the values, as expected ('THING' | 'THING_TWO'), but when using typeof to get the type of one of these properties, we get a type of string. I'd expect to get the string literal type instead.

const ENUM = Object.freeze({
    THING: 'THING',
    THING_TWO: 'THING_TWO',
});

const notAThing: typeof ENUM.THING = 'NOT_A_THING' // no error

const isAThing: $Values<typeof ENUM> = 'IS_A_THING' // Cannot assign `'IS_A_THING'` to `isAThing` because string [1] is incompatible with enum [2].

Try Flow

Of course, typing each property individually works, but I'd expect Object.freeze() to produce the same output. Is there another way around this?

@goodmind goodmind linked a pull request Jul 23, 2019 that will close this issue
goodmind added a commit to goodmind/flow that referenced this issue Jul 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants