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

Const assertions #7970

Closed
goodmind opened this issue Jul 26, 2019 · 6 comments
Closed

Const assertions #7970

goodmind opened this issue Jul 26, 2019 · 6 comments

Comments

@goodmind
Copy link
Contributor

goodmind commented Jul 26, 2019

Proposal

https://devblogs.microsoft.com/typescript/announcing-typescript-3-4/#const-assertions

const foo = ([1, 2, 3]: const) // Type is [+1, +2, +3] (how to do read-only elements on tuple?)
const bar = ('foo': const) // Type is 'foo'
const baz = ({foo: 1}: const) // Type is '{| +foo: 1 |}'

Use case

More explicit version of #7607
Also would eliminate need for Object.freeze as tool for casting to singleton strings
Object.freeze is also a bit broken with regards to subtyping #7964

Not sure how sound it is, given that it is type cast essentially

Alternative

const foo = Object.freeze('foo') // Type is 'foo'
const baz = Object.freeze(1) // Type is 1
const bar = Object.freeze([1, 2, 3]) // Type is [+1, +2, +3] (how to do read-only elements on tuple?)
@kevinbarabash
Copy link

This would be really handy for objects as well. Some of our redux code at work defines constants objects like so:

const Constants = {
   Foo: ("Foo": "Foo"),
   Bar: ("Bar": "Bar"),
   ...
};

except the constants are much longer. It would be nice to be able to do:

const Constants = ({
   Foo: "Foo",
   Bar: "Bar",
   ...,
}: const);

@goodmind
Copy link
Contributor Author

goodmind commented Aug 4, 2019

Thia is currently possible with Object.freeze, but only $Values supports it

@kevinbarabash
Copy link

Does this mean $Values would return a tuple of type ["Foo", "Bar", ...]?

@goodmind
Copy link
Contributor Author

goodmind commented Aug 4, 2019

@kevinbarabash, no, union

@Eliav2
Copy link

Eliav2 commented Oct 13, 2021

something new here? this static type checker really has some potential but these missing typescript equivalent features prevent many of us from trying flow seriously...

@SamChou19815
Copy link
Contributor

as const support is now enabled by default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants