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

Suggestion: Infer literal types for properties of "frozen" object #29603

Closed
4 of 5 tasks
levenleven opened this issue Jan 26, 2019 · 5 comments
Closed
4 of 5 tasks

Suggestion: Infer literal types for properties of "frozen" object #29603

levenleven opened this issue Jan 26, 2019 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@levenleven
Copy link

Search Terms

Object.freeze, literal types

Suggestion

Infer literal types for properties of "frozen" object. See example below

Use Cases

Moving project from flow to typescript. Common pattern to define enums in flow is freezing an object and using union of a value types as the enum type:

const foo = Object.freeze({ a: 'a', b: 'b' });
type FooValue = $Values<typeof foo>; // --> 'a' | 'b'

Currently same pattern in typescript will result in string.

Examples

const foo = Object.freeze({ a: 'a', b: 'b' });
type Foo = typeof foo; // Expected: { readonly a: 'a'; readonly b: 'b'; } Actual: { readonly a: string; readonly b: string; }
type FooValue = Foo[keyof Foo] // Expected: 'a' | 'b' Actual: 'string'

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code (Not sure about that)
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@fatcerberus
Copy link

Do note that TypeScript has native strongly-typed enums built in:
https://www.typescriptlang.org/docs/handbook/enums.html#main-content

Something like this might be nice to have when typechecking JS code (--checkJs) though.

@levenleven
Copy link
Author

Sure, enums are just example. Once object frozen, its values can't be changed, so it makes sense to infer their types as lietrals

@NN---
Copy link

NN--- commented Jan 26, 2019

I guess this one will solve it once merged #29510

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 29, 2019
@levenleven
Copy link
Author

@RyanCavanaugh just to clarify duplicate of #29510?

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants