-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Support 'as const' together with type restriction #32043
Comments
Your type |
Helper function to the rescue! type MyType = { name: string };
const asMyType = <T extends MyType>(x: T) => x; // helper function
const x = asMyType({ name: "test" } as const); // 😃 |
@jcalz this does the trick. @MartinJohns readonly was not originally of my concern, but emerged with 'as const'. Seems with jcalz's solution readony does not emerge. Now a single string literal type should be readonly by implication though... well you are allowed to overwrite with the same string I guess. |
@KnutRyagerInmeta You mean a string literal type, e.g. With jcalz solution you will have the same issue when you use the wrong type:
But I likely misunderstand you, and you want something like "make sure this object matches SomeType, then make it a const context", in which case jcalz solution is probably the best. |
Would be nice to be able to apply type checking to 'as const' types, currently I have to use the silly trick of using type checks when writing them, then switch to 'as const' afterwards.
The text was updated successfully, but these errors were encountered: