Skip to content

Type inference does not work for enums and calculated props #28527

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

Open
pm-nsimic opened this issue Nov 14, 2018 · 2 comments
Open

Type inference does not work for enums and calculated props #28527

pm-nsimic opened this issue Nov 14, 2018 · 2 comments
Assignees
Labels
Bug A bug in TypeScript
Milestone

Comments

@pm-nsimic
Copy link

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.

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

@weswigham weswigham added the Bug A bug in TypeScript label Nov 14, 2018
@weswigham weswigham self-assigned this Dec 8, 2018
@weswigham weswigham added this to the TypeScript 3.3 milestone Dec 8, 2018
@weswigham
Copy link
Member

Ref #26797 which if it doesn't already fix this, should be able to fix this with only small modifications.

@RyanCavanaugh RyanCavanaugh removed this from the TypeScript 3.3 milestone Feb 1, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants