microsoft / microsoft/TypeScript
Type inference does not work for enums and calculated props
Open
@weswigham is already working on this.
Since Dec 8, 2018.
Bug
Domain: check: Type Inference
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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.
Actual behavior:
There is no typescript error on the object doesntThrow. The resulting object of type TKeyedByEnum contains values of type number.
Related Issues:
https://github.com/Microsoft/TypeScript/issues/27704
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.