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

Support 'as const' together with type restriction #32043

Closed
KnutRyagerInmeta opened this issue Jun 22, 2019 · 4 comments
Closed

Support 'as const' together with type restriction #32043

KnutRyagerInmeta opened this issue Jun 22, 2019 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@KnutRyagerInmeta
Copy link

KnutRyagerInmeta commented Jun 22, 2019

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.

type MyType = {name: string};

const x:MyType = {
    name: 'test' // Autocompleted, typesafe. But Type is {name: string}, not 
                 // what I want, I want {readonly name: 'test'}
} as const;

const x = { name: 'test' } as const; // Gives correct type, but no type check for MyType...
@KnutRyagerInmeta KnutRyagerInmeta changed the title Support 'as const' together with type Support 'as const' together with type restriction Jun 22, 2019
@MartinJohns
Copy link
Contributor

Your type MyType clearly says that name is a mutable property. If you want it read-only, then you can just use Readonly<MyType>.

@jcalz
Copy link
Contributor

jcalz commented Jun 23, 2019

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); // 😃

@KnutRyagerInmeta
Copy link
Author

@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.

@MartinJohns
Copy link
Contributor

@KnutRyagerInmeta You mean a string literal type, e.g. 'fixed' instead of string. In that case the same logic applies: Your type MyType explicitly says the property name can be of any string.

With jcalz solution you will have the same issue when you use the wrong type:

const x: MyType = asMyType({ name: "test" } as const); // x.name is of type string again

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.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jun 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants