We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
TypeScript Version: 3.1.6
Calculated props, Spread operator, Enum keyed types
Code
enum Test { param = 'X', other = 'Y', last = 'Z', }; type TSomeOtherType = { readonly someProp: number; readonly orOther: string; }; type TKeyedByEnum = { [key in Test]: TSomeOtherType | null; }; // const throwsCompilerError: TKeyedByEnum = { // [Test.param]: 123, // }; Object.keys(Test).reduce( (acc, key: Test): TKeyedByEnum => { const doesntThrow: TKeyedByEnum = { ...acc, [key]: 123, }; return doesntThrow; }, {} as TKeyedByEnum );
Expected behavior:
Typescript compiler throws an error on the object doesntThrow.
doesntThrow
Actual behavior:
There is no typescript error on the object doesntThrow. The resulting object of type TKeyedByEnum contains values of type number.
TKeyedByEnum
number
Playground Link: http://www.typescriptlang.org/play/#src=enum%20Test%20%7B%0D%0A%20%20param%20%3D%20'X'%2C%0D%0A%20%20other%20%3D%20'Y'%2C%0D%0A%20%20last%20%3D%20'Z'%2C%0D%0A%7D%3B%0D%0A%0D%0Atype%20TSomeOtherType%20%3D%20%7B%0D%0A%20%20readonly%20someProp%3A%20number%3B%0D%0A%20%20readonly%20orOther%3A%20string%3B%0D%0A%7D%3B%0D%0A%0D%0Atype%20TKeyedByEnum%20%3D%20%7B%0D%0A%20%20%5Bkey%20in%20Test%5D%3A%20TSomeOtherType%20%7C%20null%3B%0D%0A%7D%3B%0D%0A%0D%0Aconst%20throwsCompilerError%3A%20TKeyedByEnum%20%3D%20%7B%0D%0A%20%20%5BTest.param%5D%3A%20123%2C%0D%0A%7D%3B%0D%0A%0D%0Aconst%20t%20%3D%20Object.keys(Test).reduce(%0D%0A%20%20%20%20(acc%2C%20key%3A%20Test)%3A%20TKeyedByEnum%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20const%20doesntThrowCompilerError%3A%20TKeyedByEnum%20%3D%20%7B%0D%0A%20%20%20%20%20%20%20%20...acc%2C%0D%0A%20%20%20%20%20%20%20%20%5Bkey%5D%3A%20123%2C%0D%0A%20%20%20%20%20%20%7D%3B%0D%0A%20%20%20%20%20%20return%20doesntThrowCompilerError%3B%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%7D%20as%20TKeyedByEnum%0D%0A)%3B%0D%0A%0D%0Aconsole.log(t)%3B
Related Issues: #27704
The text was updated successfully, but these errors were encountered:
Ref #26797 which if it doesn't already fix this, should be able to fix this with only small modifications.
Sorry, something went wrong.
Just ran into a version of this https://www.typescriptlang.org/play/index.html#code/MYewdgzgLgBApmArgWxgMRCGBvAUDAmAQRgF4YAiIigGlwF9ddRJYo5oAuGAJTlABOAEwA8GEDRhJkAIzgCAfGRyVq3AIwx6AbiYsIIADZwAdIZABzABTtoAbXEmiAXQCUu3ADMQAmFf2wANZwAJ4wAJZgMLZQrjj4hPpGpubWMXbBIW669EA
weswigham
No branches or pull requests
TypeScript Version: 3.1.6
Calculated props, Spread operator, Enum keyed types
Code
Expected behavior:
Typescript compiler throws an error on the object
doesntThrow
.Actual behavior:
There is no typescript error on the object
doesntThrow
. The resulting object of typeTKeyedByEnum
contains values of typenumber
.Playground Link:
http://www.typescriptlang.org/play/#src=enum%20Test%20%7B%0D%0A%20%20param%20%3D%20'X'%2C%0D%0A%20%20other%20%3D%20'Y'%2C%0D%0A%20%20last%20%3D%20'Z'%2C%0D%0A%7D%3B%0D%0A%0D%0Atype%20TSomeOtherType%20%3D%20%7B%0D%0A%20%20readonly%20someProp%3A%20number%3B%0D%0A%20%20readonly%20orOther%3A%20string%3B%0D%0A%7D%3B%0D%0A%0D%0Atype%20TKeyedByEnum%20%3D%20%7B%0D%0A%20%20%5Bkey%20in%20Test%5D%3A%20TSomeOtherType%20%7C%20null%3B%0D%0A%7D%3B%0D%0A%0D%0Aconst%20throwsCompilerError%3A%20TKeyedByEnum%20%3D%20%7B%0D%0A%20%20%5BTest.param%5D%3A%20123%2C%0D%0A%7D%3B%0D%0A%0D%0Aconst%20t%20%3D%20Object.keys(Test).reduce(%0D%0A%20%20%20%20(acc%2C%20key%3A%20Test)%3A%20TKeyedByEnum%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20const%20doesntThrowCompilerError%3A%20TKeyedByEnum%20%3D%20%7B%0D%0A%20%20%20%20%20%20%20%20...acc%2C%0D%0A%20%20%20%20%20%20%20%20%5Bkey%5D%3A%20123%2C%0D%0A%20%20%20%20%20%20%7D%3B%0D%0A%20%20%20%20%20%20return%20doesntThrowCompilerError%3B%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%7D%20as%20TKeyedByEnum%0D%0A)%3B%0D%0A%0D%0Aconsole.log(t)%3B
Related Issues:
#27704
The text was updated successfully, but these errors were encountered: