Skip to content

Mapped types regression with v3.6.2 #33210

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

Closed
lemoustachiste opened this issue Sep 3, 2019 · 3 comments
Closed

Mapped types regression with v3.6.2 #33210

lemoustachiste opened this issue Sep 3, 2019 · 3 comments

Comments

@lemoustachiste
Copy link

TypeScript Version: 3.6.2

Search Terms: Mapped Types, MappedTypes, MappedType

Code

export enum LayoutElementType {
  Image = 'image' as any
}

const layoutElementTypesMap: { [key in LayoutElementType]: string } = {
  'image': PLATFORM.moduleName('components/record/record-layout/element/image.html')
};

Expected behavior: as described here: #2491 (comment), this should be a work around to map an enum to object keys. We were using it successfully until v3.5.3 (I just installed v3.5.3 to double check and no error).

Actual behavior: Typescript complains:
Object literal may only specify known properties, and ''image'' does not exist in type '{ [x: number]: string; }'.
Looks similar to this: #2491 (comment)

Playground Link:

Related Issues: #2491

@j-oliveras
Copy link
Contributor

j-oliveras commented Sep 3, 2019

If you remove the as any on the enum should work. I don't known the reason to use it. Typescript supports enums with string values since version 2.4. Full code:

export enum LayoutElementType {
  Image = 'image'
}

const layoutElementTypesMap: { [key in LayoutElementType]: string } = {
  'image': PLATFORM.moduleName('components/record/record-layout/element/image.html')
};

@jack-williams
Copy link
Collaborator

Caused by #31784.

I'm not sure why the any assertion is used in your enum, but it causes the enum to be considered a numeric enum which is in turn affected by the linked PR.

Prior to #31784, the type of layoutElementTypesMap was the empty object type and not subject to excess property checking.

@lemoustachiste
Copy link
Author

Perfect thanks @jack-williams and @j-oliveras, this does indeed fix it.
This code is legacy and written before I worked on it and I wasn't sure why too it was there, but legacy being legacy, I tend to leave things that look weird but work. Good cleanup then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants