Infer into recursive mapped type targets (#53647)

This commit is contained in:
Mateusz Burzyński
2023-07-06 13:46:46 -07:00
committed by GitHub
parent a05699c9b9
commit bd61cbb1f3
6 changed files with 195 additions and 19 deletions
+10 -9
View File
@@ -2068,7 +2068,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
/** Key is "/path/to/a.ts|/path/to/b.ts". */
var amalgamatedDuplicates: Map<string, DuplicateInfoForFiles> | undefined;
var reverseMappedCache = new Map<string, Type | undefined>();
var inInferTypeForHomomorphicMappedType = false;
var homomorphicMappedTypeInferenceStack: string[] = [];
var ambientModulesCache: Symbol[] | undefined;
/**
* List of every ambient module with a "*" wildcard.
@@ -24126,17 +24126,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
* 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, constraint: IndexType): Type | undefined {
if (inInferTypeForHomomorphicMappedType) {
const cacheKey = source.id + "," + target.id + "," + constraint.id;
if (reverseMappedCache.has(cacheKey)) {
return reverseMappedCache.get(cacheKey);
}
const recursionKey = source.id + "," + (target.target || target).id;
if (contains(homomorphicMappedTypeInferenceStack, recursionKey)) {
return undefined;
}
const key = source.id + "," + target.id + "," + constraint.id;
if (reverseMappedCache.has(key)) {
return reverseMappedCache.get(key);
}
inInferTypeForHomomorphicMappedType = true;
homomorphicMappedTypeInferenceStack.push(recursionKey);
const type = createReverseMappedType(source, target, constraint);
inInferTypeForHomomorphicMappedType = false;
reverseMappedCache.set(key, type);
homomorphicMappedTypeInferenceStack.pop();
reverseMappedCache.set(cacheKey, type);
return type;
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,73 @@
//// [tests/cases/compiler/mappedTypeRecursiveInference2.ts] ////
=== mappedTypeRecursiveInference2.ts ===
type MorphTuple = [string, "|>", any]
>MorphTuple : Symbol(MorphTuple, Decl(mappedTypeRecursiveInference2.ts, 0, 0))
type validateMorph<def extends MorphTuple> = def[1] extends "|>"
>validateMorph : Symbol(validateMorph, Decl(mappedTypeRecursiveInference2.ts, 0, 37))
>def : Symbol(def, Decl(mappedTypeRecursiveInference2.ts, 2, 19))
>MorphTuple : Symbol(MorphTuple, Decl(mappedTypeRecursiveInference2.ts, 0, 0))
>def : Symbol(def, Decl(mappedTypeRecursiveInference2.ts, 2, 19))
? [validateDefinition<def[0]>, "|>", (In: def[0]) => unknown]
>validateDefinition : Symbol(validateDefinition, Decl(mappedTypeRecursiveInference2.ts, 4, 9))
>def : Symbol(def, Decl(mappedTypeRecursiveInference2.ts, 2, 19))
>In : Symbol(In, Decl(mappedTypeRecursiveInference2.ts, 3, 42))
>def : Symbol(def, Decl(mappedTypeRecursiveInference2.ts, 2, 19))
: def
>def : Symbol(def, Decl(mappedTypeRecursiveInference2.ts, 2, 19))
type validateDefinition<def> = def extends MorphTuple
>validateDefinition : Symbol(validateDefinition, Decl(mappedTypeRecursiveInference2.ts, 4, 9))
>def : Symbol(def, Decl(mappedTypeRecursiveInference2.ts, 6, 24))
>def : Symbol(def, Decl(mappedTypeRecursiveInference2.ts, 6, 24))
>MorphTuple : Symbol(MorphTuple, Decl(mappedTypeRecursiveInference2.ts, 0, 0))
? validateMorph<def>
>validateMorph : Symbol(validateMorph, Decl(mappedTypeRecursiveInference2.ts, 0, 37))
>def : Symbol(def, Decl(mappedTypeRecursiveInference2.ts, 6, 24))
: {
[k in keyof def]: validateDefinition<def[k]>
>k : Symbol(k, Decl(mappedTypeRecursiveInference2.ts, 9, 11))
>def : Symbol(def, Decl(mappedTypeRecursiveInference2.ts, 6, 24))
>validateDefinition : Symbol(validateDefinition, Decl(mappedTypeRecursiveInference2.ts, 4, 9))
>def : Symbol(def, Decl(mappedTypeRecursiveInference2.ts, 6, 24))
>k : Symbol(k, Decl(mappedTypeRecursiveInference2.ts, 9, 11))
}
declare function type<def>(def: validateDefinition<def>): def
>type : Symbol(type, Decl(mappedTypeRecursiveInference2.ts, 10, 7))
>def : Symbol(def, Decl(mappedTypeRecursiveInference2.ts, 12, 22), Decl(mappedTypeRecursiveInference2.ts, 12, 27))
>def : Symbol(def, Decl(mappedTypeRecursiveInference2.ts, 12, 22), Decl(mappedTypeRecursiveInference2.ts, 12, 27))
>validateDefinition : Symbol(validateDefinition, Decl(mappedTypeRecursiveInference2.ts, 4, 9))
>def : Symbol(def, Decl(mappedTypeRecursiveInference2.ts, 12, 22), Decl(mappedTypeRecursiveInference2.ts, 12, 27))
>def : Symbol(def, Decl(mappedTypeRecursiveInference2.ts, 12, 22), Decl(mappedTypeRecursiveInference2.ts, 12, 27))
const shallow = type(["ark", "|>", (x) => x.length])
>shallow : Symbol(shallow, Decl(mappedTypeRecursiveInference2.ts, 14, 5))
>type : Symbol(type, Decl(mappedTypeRecursiveInference2.ts, 10, 7))
>x : Symbol(x, Decl(mappedTypeRecursiveInference2.ts, 14, 36))
>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(mappedTypeRecursiveInference2.ts, 14, 36))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
const objectLiteral = type({ a: ["ark", "|>", (x) => x.length] })
>objectLiteral : Symbol(objectLiteral, Decl(mappedTypeRecursiveInference2.ts, 15, 5))
>type : Symbol(type, Decl(mappedTypeRecursiveInference2.ts, 10, 7))
>a : Symbol(a, Decl(mappedTypeRecursiveInference2.ts, 15, 28))
>x : Symbol(x, Decl(mappedTypeRecursiveInference2.ts, 15, 47))
>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(mappedTypeRecursiveInference2.ts, 15, 47))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
const nestedTuple = type([["ark", "|>", (x) => x.length]])
>nestedTuple : Symbol(nestedTuple, Decl(mappedTypeRecursiveInference2.ts, 16, 5))
>type : Symbol(type, Decl(mappedTypeRecursiveInference2.ts, 10, 7))
>x : Symbol(x, Decl(mappedTypeRecursiveInference2.ts, 16, 41))
>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(mappedTypeRecursiveInference2.ts, 16, 41))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
@@ -0,0 +1,68 @@
//// [tests/cases/compiler/mappedTypeRecursiveInference2.ts] ////
=== mappedTypeRecursiveInference2.ts ===
type MorphTuple = [string, "|>", any]
>MorphTuple : [string, "|>", any]
type validateMorph<def extends MorphTuple> = def[1] extends "|>"
>validateMorph : validateMorph<def>
? [validateDefinition<def[0]>, "|>", (In: def[0]) => unknown]
>In : def[0]
: def
type validateDefinition<def> = def extends MorphTuple
>validateDefinition : validateDefinition<def>
? validateMorph<def>
: {
[k in keyof def]: validateDefinition<def[k]>
}
declare function type<def>(def: validateDefinition<def>): def
>type : <def>(def: validateDefinition<def>) => def
>def : validateDefinition<def>
const shallow = type(["ark", "|>", (x) => x.length])
>shallow : ["ark", "|>", (x: "ark") => number]
>type(["ark", "|>", (x) => x.length]) : ["ark", "|>", (x: "ark") => number]
>type : <def>(def: validateDefinition<def>) => def
>["ark", "|>", (x) => x.length] : ["ark", "|>", (x: "ark") => number]
>"ark" : "ark"
>"|>" : "|>"
>(x) => x.length : (x: "ark") => number
>x : "ark"
>x.length : number
>x : "ark"
>length : number
const objectLiteral = type({ a: ["ark", "|>", (x) => x.length] })
>objectLiteral : { a: ["ark", "|>", (x: "ark") => number]; }
>type({ a: ["ark", "|>", (x) => x.length] }) : { a: ["ark", "|>", (x: "ark") => number]; }
>type : <def>(def: validateDefinition<def>) => def
>{ a: ["ark", "|>", (x) => x.length] } : { a: ["ark", "|>", (x: "ark") => number]; }
>a : ["ark", "|>", (x: "ark") => number]
>["ark", "|>", (x) => x.length] : ["ark", "|>", (x: "ark") => number]
>"ark" : "ark"
>"|>" : "|>"
>(x) => x.length : (x: "ark") => number
>x : "ark"
>x.length : number
>x : "ark"
>length : number
const nestedTuple = type([["ark", "|>", (x) => x.length]])
>nestedTuple : [["ark", "|>", (x: "ark") => number]]
>type([["ark", "|>", (x) => x.length]]) : [["ark", "|>", (x: "ark") => number]]
>type : <def>(def: validateDefinition<def>) => def
>[["ark", "|>", (x) => x.length]] : [["ark", "|>", (x: "ark") => number]]
>["ark", "|>", (x) => x.length] : ["ark", "|>", (x: "ark") => number]
>"ark" : "ark"
>"|>" : "|>"
>(x) => x.length : (x: "ark") => number
>x : "ark"
>x.length : number
>x : "ark"
>length : number
@@ -0,0 +1,20 @@
// @strict: true
// @noEmit: true
type MorphTuple = [string, "|>", any]
type validateMorph<def extends MorphTuple> = def[1] extends "|>"
? [validateDefinition<def[0]>, "|>", (In: def[0]) => unknown]
: def
type validateDefinition<def> = def extends MorphTuple
? validateMorph<def>
: {
[k in keyof def]: validateDefinition<def[k]>
}
declare function type<def>(def: validateDefinition<def>): def
const shallow = type(["ark", "|>", (x) => x.length])
const objectLiteral = type({ a: ["ark", "|>", (x) => x.length] })
const nestedTuple = type([["ark", "|>", (x) => x.length]])