Skip to content

Additional tweak to mapped type property modifier propagation #12843

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

Merged
merged 3 commits into from
Dec 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4577,12 +4577,22 @@ namespace ts {

function getModifiersTypeFromMappedType(type: MappedType) {
if (!type.modifiersType) {
// If the mapped type was declared as { [P in keyof T]: X } or as { [P in K]: X }, where
// K is constrained to 'K extends keyof T', then we will copy property modifiers from T.
const declaredType = <MappedType>getTypeFromMappedTypeNode(type.declaration);
const constraint = getConstraintTypeFromMappedType(declaredType);
const extendedConstraint = constraint.flags & TypeFlags.TypeParameter ? getConstraintOfTypeParameter(<TypeParameter>constraint) : constraint;
type.modifiersType = extendedConstraint.flags & TypeFlags.Index ? instantiateType((<IndexType>extendedConstraint).type, type.mapper || identityMapper) : emptyObjectType;
const constraintDeclaration = type.declaration.typeParameter.constraint;
if (constraintDeclaration.kind === SyntaxKind.TypeOperator) {
// If the constraint declaration is a 'keyof T' node, the modifiers type is T. We check
// AST nodes here because, when T is a non-generic type, the logic below eagerly resolves
// 'keyof T' to a literal union type and we can't recover T from that type.
type.modifiersType = instantiateType(getTypeFromTypeNode((<TypeOperatorNode>constraintDeclaration).type), type.mapper || identityMapper);
}
else {
// Otherwise, get the declared constraint type, and if the constraint type is a type parameter,
// get the constraint of that type parameter. If the resulting type is an indexed type 'keyof T',
// the modifiers type is T. Otherwise, the modifiers type is {}.
const declaredType = <MappedType>getTypeFromMappedTypeNode(type.declaration);
const constraint = getConstraintTypeFromMappedType(declaredType);
const extendedConstraint = constraint.flags & TypeFlags.TypeParameter ? getConstraintOfTypeParameter(<TypeParameter>constraint) : constraint;
type.modifiersType = extendedConstraint.flags & TypeFlags.Index ? instantiateType((<IndexType>extendedConstraint).type, type.mapper || identityMapper) : emptyObjectType;
}
}
return type.modifiersType;
}
Expand Down
22 changes: 17 additions & 5 deletions tests/baselines/reference/mappedTypeModifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ var v01: Pick<Pick<T, keyof T>, keyof T>;
var v02: TP;
var v02: { [P in keyof T]?: T[P] };
var v02: Partial<T>;
var v02: Pick<TP, keyof T>;
var v02: { [P in keyof TP]: TP[P] }
var v02: Pick<TP, keyof TP>;

var v03: TR;
var v03: { readonly [P in keyof T]: T[P] };
var v03: Readonly<T>;
var v03: Pick<TR, keyof T>;
var v03: { [P in keyof TR]: TR[P] }
var v03: Pick<TR, keyof TR>;

var v04: TPR;
var v04: { readonly [P in keyof T]?: T[P] };
var v04: Partial<TR>;
var v04: Readonly<TP>;
var v04: Partial<Readonly<T>>;
var v04: Readonly<Partial<T>>;
var v04: { [P in keyof TPR]: TPR[P] }
var v04: Pick<TPR, keyof T>;

type Boxified<T> = { [P in keyof T]: { x: T[P] } };
Expand All @@ -55,20 +58,23 @@ var b01: Pick<Pick<B, keyof B>, keyof B>;
var b02: BP;
var b02: { [P in keyof B]?: B[P] };
var b02: Partial<B>;
var b02: Pick<BP, keyof B>;
var b02: { [P in keyof BP]: BP[P] }
var b02: Pick<BP, keyof BP>;

var b03: BR;
var b03: { readonly [P in keyof B]: B[P] };
var b03: Readonly<B>;
var b03: Pick<BR, keyof B>;
var b03: { [P in keyof BR]: BR[P] }
var b03: Pick<BR, keyof BR>;

var b04: BPR;
var b04: { readonly [P in keyof B]?: B[P] };
var b04: Partial<BR>;
var b04: Readonly<BP>;
var b04: Partial<Readonly<B>>;
var b04: Readonly<Partial<B>>;
var b04: Pick<BPR, keyof B>;
var b04: { [P in keyof BPR]: BPR[P] }
var b04: Pick<BPR, keyof BPR>;

//// [mappedTypeModifiers.js]
var v00;
Expand All @@ -84,6 +90,8 @@ var v02;
var v02;
var v02;
var v02;
var v02;
var v03;
var v03;
var v03;
var v03;
Expand All @@ -95,6 +103,7 @@ var v04;
var v04;
var v04;
var v04;
var v04;
var b00;
var b00;
var b00;
Expand All @@ -108,6 +117,8 @@ var b02;
var b02;
var b02;
var b02;
var b02;
var b03;
var b03;
var b03;
var b03;
Expand All @@ -119,3 +130,4 @@ var b04;
var b04;
var b04;
var b04;
var b04;
Loading