Skip to content

Conversation

@ahejlsberg
Copy link
Member

This PR improves our handling of readonly and optional modifiers in mapped type inference. When inferring to a homomorphic (structure preserving) mapped type {readonly [P in keyof T]: X }, no readonly modifiers will be present in the type inferred for T (because it is known that the mapping will add it). Likewise, when inferring to a mapped type {[P in keyof T]?: X }, no ? modifers will be present in the type inferred for T. In effect, readonly and optional modifiers in the target are used to indicate that those modifiers should be stripped in the inferred type.

declarefunctionvalidate<T>(obj: {[PinkeyofT]?: T[P]}): T;declarefunctionclone<T>(obj: {readonly[PinkeyofT]: T[P]}): T;declarefunctionvalidateAndClone<T>(obj: {readonly[PinkeyofT]?: T[P]}): T;typeFoo={a?: number;readonlyb: string;}functiontest(foo: Foo){letx=validate(foo);//{a: number, readonly b: string }lety=clone(foo);//{a?: number, b: string }letz=validateAndClone(foo);//{a: number, b: string }}

@ahejlsberg
Copy link
MemberAuthor

@mhegazy@DanielRosenwasser This is effectively the inference side of #12563.

@mhegazy
Copy link
Contributor

nice. just ran into this while replying to #12578 (comment).

@ahejlsbergahejlsberg merged commit 030da0b into masterNov 30, 2016
@ahejlsbergahejlsberg deleted the mappedTypeModifierInference branch November 30, 2016 19:11
This was referenced Nov 30, 2016
@mhegazymhegazy mentioned this pull request Dec 1, 2016
@microsoftmicrosoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@ahejlsberg@mhegazy@msftclas