You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can't assign existing property to the rest of generic #28952
interfaceAB{a: string;b: string;}functionexcludeA<TextendsAB>(obj: T){const{a, ...rest}=obj;rest.b='3';returnrest;}interfaceExactlyABextendsAB{a: "a";b: "b"}declarevarexactAB: ExactlyAB;excludeA(exactAB).b;// claims to be of type "b", but will be the value "3" with your implementation
(But in general yes, #28821 is the reason why the assignment doesn't work, even when you correctly assign a T["b"] - I just wanted to let you know that that code is unsafe from a typesystem perspective, even once that bug is fixed)
Yea - it's a known hole that gets closed when you use indexed access types (which get automatically made when you fetch properties off mapped types). It's quite troublesome, really, since the only reason we haven't fixed it, AFAIK, is for backwards compatibility (and to make mutating patterns "easier" to type).
Activity
weswigham commentedon Dec 11, 2018
Pavel Klimashkin (@klimashkin) - Consider:
weswigham commentedon Dec 11, 2018
(But in general yes, #28821 is the reason why the assignment doesn't work, even when you correctly assign a
T["b"]- I just wanted to let you know that that code is unsafe from a typesystem perspective, even once that bug is fixed)que-etc commentedon Dec 11, 2018
Wesley Wigham (@weswigham)
Does it mean that any reassignment of an object's property, which has a generic type, should be considered invalid?
Playground.
weswigham commentedon Dec 12, 2018
Yea - it's a known hole that gets closed when you use indexed access types (which get automatically made when you fetch properties off mapped types). It's quite troublesome, really, since the only reason we haven't fixed it, AFAIK, is for backwards compatibility (and to make mutating patterns "easier" to type).
typescript-bot commentedon Dec 13, 2018
This issue has been marked as a duplicate and has seen no activity in the last day. It has been closed for automatic house-keeping purposes.
klimashkin commentedon Sep 5, 2019
The issue has been fixed:
http://www.typescriptlang.org/play/?allowJs=true&target=6#code/JYOwLgpgTgZghgYwgAgJIHkBGArZBvAKGWOTgC5kQBXAW02gG4iTMLq7HniE3b6omAXwIEYVEAjDAA9iGSQAzmAA8AFWQQAHpBAATBWizYANMgDSG7RD0H1AMnzIAXrw4DkggHwAKaTgqqAJQU6DTAKmamAORwUcgAPshRmFGexIQkyAiySvhwppimAHQlUBBKgsgAvMh+uHAGZkxcyGVKRU7VyADMAEzNmWVgVFBybWBCIopg3njkyACMBRS9pjw9proUACyCgUW6DEA