Skip to content
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

Mapped generic types lose index signature #23080

Closed
tyrielv opened this issue Apr 2, 2018 · 5 comments
Closed

Mapped generic types lose index signature #23080

tyrielv opened this issue Apr 2, 2018 · 5 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@tyrielv
Copy link

tyrielv commented Apr 2, 2018

TypeScript Version: 2.9.0-dev.20180402

Search Terms: partial mapped types index signature enum

Code

enum Fruits {
    Mango = 0,
    Banana = 1
}

interface IFruitMetadata<T> {
    [Fruits.Mango]: T;
    [Fruits.Banana]: T;
}

const onSale1: IFruitMetadata<boolean> = {
    [Fruits.Mango]: true,
    [Fruits.Banana]: false
};

const onSale2: Partial<IFruitMetadata<boolean>> = {
    [Fruits.Banana]: true,
};

const onSale3: Pick<IFruitMetadata<boolean>, Fruits.Banana> = {
    [Fruits.Banana]: true,
};

onSale1[Fruits.Banana] = true;
onSale2[Fruits.Mango] = true;
onSale3[Fruits.Banana] = true;

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.

@tyrielv tyrielv changed the title Mapped types lose index signature Mapped generic types lose index signature Apr 2, 2018
@mhegazy mhegazy added this to the TypeScript 2.9 milestone Apr 3, 2018
@mhegazy mhegazy added the Bug A bug in TypeScript label Apr 3, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Apr 3, 2018

@weswigham this should be fixed by #23090, correct?

@weswigham
Copy link
Member

I think so? But @ahejlsberg was unenthused with that fix and was looking for a more holistic approach to the problems and inconsistencies we have in the keyof area in general. (Though I may have a revision that's a little closer to acceptable)

@mhegazy
Copy link
Contributor

mhegazy commented Apr 3, 2018

Spoke to @ahejlsberg, he has a different proposal for adding a new indexof operator. should address both issues.

@dardino
Copy link

dardino commented Apr 23, 2018

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**

@mhegazy
Copy link
Contributor

mhegazy commented Apr 26, 2018

Sample described in OP should be fixed by #23592.

@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
};

@mhegazy mhegazy closed this as completed Apr 26, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Apr 26, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants