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 bug with modifiers #13724

Closed
marcomura opened this issue Jan 27, 2017 · 6 comments
Closed

Mapped Type bug with modifiers #13724

marcomura opened this issue Jan 27, 2017 · 6 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@marcomura
Copy link

Putting parenthesis around the "keyof T" syntax in an inline Mapped Type, strips out the field modifier (readonly).
Without parenthesis, or defining a template using the parenthesis, this doesn't happen.

TypeScript Version: 2.1.1

Code

interface MyType {
  readonly a: number;
  b: string;
}

type MyType2 = {
  [P in keyof MyType]: MyType[P];
}

type MyType3 = {
  [P in (keyof MyType)]: MyType[P];
}

type Test<T> = {
  [P in (keyof T)]: T[P];
}
type MyType4 = Test<MyType>;

Expected behavior:
MyType2, MyType3, and MyType4 to all have the same signature.

Actual behavior:
MyType2 and MyType4 keeps the original MyType signature with modifiers.
MyType3 strips out the modifiers:

{
  a: number;
  b: string;
}
@mhegazy
Copy link
Contributor

mhegazy commented Jan 27, 2017

This should be fixed in TS 2.1.5

@mhegazy mhegazy added the Bug A bug in TypeScript label Jan 27, 2017
@nitzantomer
Copy link

May I ask what exactly is the bug?
Is it supposed to work like it does with the parenthesis or without?

If it should keep the readonly modifiers, how can you create a writeable version of a readonly type?

@marcomura
Copy link
Author

I guess it's supposed to work like it does without parenthesis, but I was more looking for consistency here.
Anyway, I agree that right now we lack the possibility of removing the readonly, even if mhegazy explained a workaround here:
https://github.com/Microsoft/TypeScript/issues/13723

Unfortunately, that workaround also removes the optional attribute, and it can't be declared to work recursively :(

@yortus
Copy link
Contributor

yortus commented Feb 20, 2018

Bump. I just ran into this. Will this bug be getting a milestone?

@mhegazy mhegazy added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Bug A bug in TypeScript labels Apr 12, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Apr 12, 2018

this is working as intended, it was meant as a way to allow for removal of readonly modifier.

@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 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants