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 type with enum keys produces {} #23084

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

Mapped type with enum keys produces {} #23084

tyrielv opened this issue Apr 2, 2018 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@tyrielv
Copy link

tyrielv commented Apr 2, 2018

TypeScript Version: 2.9.0-dev2.0180402

Search Terms: mapped generic type array

Code

enum Fruits {
    Mango = 0,
    Banana = 1
}

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

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

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

Expected behavior:
Both assignments have compile errors (number not assignable to boolean) (This is the behavior on 2.7.2)
Actual behavior:
Only the unmapped type assignment has a compile error.
Playground Link

Related Issues:

@ghost
Copy link

ghost commented Apr 2, 2018

The problem is that Partial<IFruitMetadata<boolean>> is {}. Simplified example:

const enum E { A }
interface I { [E.A]: number; }

type T = { [K in keyof I]: boolean }; // Fails

type Bools<T> = { [K in keyof T]: boolean };
type B = Bools<I>; // No error, returns {}

@ghost ghost added the Bug A bug in TypeScript label Apr 2, 2018
@ghost ghost changed the title Array of mapped generic type allows invalid object assignments Mapped type with enum keys produces {} Apr 2, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Apr 2, 2018

Duplicate of #13042.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 2, 2018

keyof I should be never really. but that is a different bug.

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Bug A bug in TypeScript labels Apr 2, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants