microsoft / microsoft/TypeScript

Type inference does not work for enums and calculated props

Aperta
#28,527 2 commenti 3 reazioni 1 assegnatario Vedi su GitHub

@weswigham ci sta già lavorando.

Dal 8/12/2018.

Bug Domain: check: Type Inference
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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:
https://github.com/Microsoft/TypeScript/issues/27704

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.