Skip to content
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
38 changes: 34 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10783,10 +10783,32 @@ namespace ts {
return type === typeParameter || type.flags & TypeFlags.UnionOrIntersection && forEach((<UnionOrIntersectionType>type).types, t => isTypeParameterAtTopLevel(t, typeParameter));
}

// Infer a suitable input type for a homomorphic mapped type { [P in keyof T]: X }. We construct
// an object type with the same set of properties as the source type, where the type of each
// property is computed by inferring from the source property type to X for the type
// variable T[P] (i.e. we treat the type T[P] as the type variable we're inferring for).
/** Create an object with properties named in the string literal type. Every property has type `{}` */
function createEmptyObjectTypeFromStringLiteral(type: Type) {
const members = createSymbolTable();
forEachType(type, t => {
if (!(t.flags & TypeFlags.StringLiteral)) {
return;
}
const name = escapeLeadingUnderscores((t as StringLiteralType).value);
const literalProp = createSymbol(SymbolFlags.Property, name);
literalProp.type = emptyObjectType;
if (t.symbol) {
literalProp.declarations = t.symbol.declarations;
literalProp.valueDeclaration = t.symbol.valueDeclaration;
}
members.set(name, literalProp);
});
const indexInfo = type.flags & TypeFlags.String ? createIndexInfo(emptyObjectType, /*isReadonly*/ false) : undefined;
return createAnonymousType(undefined, members, emptyArray, emptyArray, indexInfo, undefined);
}

/**
* Infer a suitable input type for a homomorphic mapped type { [P in keyof T]: X }. We construct
* an object type with the same set of properties as the source type, where the type of each
* property is computed by inferring from the source property type to X for the type
* variable T[P] (i.e. we treat the type T[P] as the type variable we're inferring for).
*/
function inferTypeForHomomorphicMappedType(source: Type, target: MappedType): Type {
const properties = getPropertiesOfType(source);
let indexInfo = getIndexInfoOfType(source, IndexKind.String);
Expand Down Expand Up @@ -10942,7 +10964,15 @@ namespace ts {
}
}
else if (source.flags & TypeFlags.Index && target.flags & TypeFlags.Index) {
priority ^= InferencePriority.Contravariant;
inferFromTypes((<IndexType>source).type, (<IndexType>target).type);
priority ^= InferencePriority.Contravariant;
}
else if ((isLiteralType(source) || source.flags & TypeFlags.String) && target.flags & TypeFlags.Index) {
const empty = createEmptyObjectTypeFromStringLiteral(source);
priority ^= InferencePriority.Contravariant;
inferFromTypes(empty, (target as IndexType).type);
priority ^= InferencePriority.Contravariant;
}
else if (source.flags & TypeFlags.IndexedAccess && target.flags & TypeFlags.IndexedAccess) {
inferFromTypes((<IndexedAccessType>source).objectType, (<IndexedAccessType>target).objectType);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//// [inferObjectTypeFromStringLiteralToKeyof.ts]
declare function inference<T>(target: T, name: keyof T): void;
declare var two: "a" | "d";
inference({ a: 1, b: 2, c: 3, d(n) { return n } }, two);


//// [inferObjectTypeFromStringLiteralToKeyof.js]
inference({ a: 1, b: 2, c: 3, d: function (n) { return n; } }, two);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/inferObjectTypeFromStringLiteralToKeyof.ts ===
declare function inference<T>(target: T, name: keyof T): void;
>inference : Symbol(inference, Decl(inferObjectTypeFromStringLiteralToKeyof.ts, 0, 0))
>T : Symbol(T, Decl(inferObjectTypeFromStringLiteralToKeyof.ts, 0, 27))
>target : Symbol(target, Decl(inferObjectTypeFromStringLiteralToKeyof.ts, 0, 30))
>T : Symbol(T, Decl(inferObjectTypeFromStringLiteralToKeyof.ts, 0, 27))
>name : Symbol(name, Decl(inferObjectTypeFromStringLiteralToKeyof.ts, 0, 40))
>T : Symbol(T, Decl(inferObjectTypeFromStringLiteralToKeyof.ts, 0, 27))

declare var two: "a" | "d";
>two : Symbol(two, Decl(inferObjectTypeFromStringLiteralToKeyof.ts, 1, 11))

inference({ a: 1, b: 2, c: 3, d(n) { return n } }, two);
>inference : Symbol(inference, Decl(inferObjectTypeFromStringLiteralToKeyof.ts, 0, 0))
>a : Symbol(a, Decl(inferObjectTypeFromStringLiteralToKeyof.ts, 2, 11))
>b : Symbol(b, Decl(inferObjectTypeFromStringLiteralToKeyof.ts, 2, 17))
>c : Symbol(c, Decl(inferObjectTypeFromStringLiteralToKeyof.ts, 2, 23))
>d : Symbol(d, Decl(inferObjectTypeFromStringLiteralToKeyof.ts, 2, 29))
>n : Symbol(n, Decl(inferObjectTypeFromStringLiteralToKeyof.ts, 2, 32))
>n : Symbol(n, Decl(inferObjectTypeFromStringLiteralToKeyof.ts, 2, 32))
>two : Symbol(two, Decl(inferObjectTypeFromStringLiteralToKeyof.ts, 1, 11))

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/inferObjectTypeFromStringLiteralToKeyof.ts ===
declare function inference<T>(target: T, name: keyof T): void;
>inference : <T>(target: T, name: keyof T) => void
>T : T
>target : T
>T : T
>name : keyof T
>T : T

declare var two: "a" | "d";
>two : "a" | "d"

inference({ a: 1, b: 2, c: 3, d(n) { return n } }, two);
>inference({ a: 1, b: 2, c: 3, d(n) { return n } }, two) : void
>inference : <T>(target: T, name: keyof T) => void
>{ a: 1, b: 2, c: 3, d(n) { return n } } : { a: number; b: number; c: number; d(n: any): any; }
>a : number
>1 : 1
>b : number
>2 : 2
>c : number
>3 : 3
>d : (n: any) => any
>n : any
>n : any
>two : "a" | "d"

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare function inference<T>(target: T, name: keyof T): void;
declare var two: "a" | "d";
inference({ a: 1, b: 2, c: 3, d(n) { return n } }, two);