Repository navigation
Mapped generic types lose index signature #23080
Copy link
Copy link
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
Milestone
Description
Activity
- changed the title
[-]Mapped types lose index signature[/-][+]Mapped generic types lose index signature[/+]on Apr 2, 2018 Wesley Wigham (@weswigham) this should be fixed by #23090, correct?
I think so? But Anders Hejlsberg (@ahejlsberg) was unenthused with that fix and was looking for a more holistic approach to the problems and inconsistencies we have in the
keyofarea in general. (Though I may have a revision that's a little closer to acceptable)Spoke to Anders Hejlsberg (@ahejlsberg), he has a different proposal for adding a new
indexofoperator. should address both issues.Another example:
enum MyEnum { ValA = 0, ValB = 1, } type IEnumTpProp<R> = {[key in keyof typeof MyEnum]: R }; var X: IEnumTpProp<string> = { ValA: "text1", ValB: "text2", 0: "error" // expected: error; current: error; //OK }; X[MyEnum[MyEnum.ValA]] = "no error"; // current: no error; expected: no error // **OK** X[MyEnum.ValA] = "no error??"; // current: no error; expected: error // **KO**
Sample described in OP should be fixed by #23592.
Gabriele (@dardino) the behavior in your example is expected, since enums have a numeric index signature.
with #23592 you can write something like:enum MyEnum { ValA = 0, ValB = 1, } type IEnumTpProp<R> = { [key in MyEnum]: R }; var X: IEnumTpProp<string> = { [MyEnum.ValA]: "text1", [MyEnum.ValB]: "text2", 0 :"" //Error, duplicate entry };
- addedFixedA PR has been merged for this issueA PR has been merged for this issue
on Apr 26, 2018 - locked and limited conversation to collaborators
on Jul 31, 2018
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
TypeScript Version: 2.9.0-dev.20180402
Search Terms: partial mapped types index signature enum
Code
Expected behavior:
No compile errors. This was the behavior in 2.7.2.
Actual behavior:
With noImplicitAny=true, the Partial and Pick versions of the type triggers error TS7017: Element implicitly has an 'any' type because type 'Partial<IFruitMetadata>' has no index signature.
Playground Link
#22892 looks like it might be related.