Skip to content

Infer into recursive mapped type targets #53647

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 4 commits into from
Jul 6, 2023
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
19 changes: 10 additions & 9 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -24119,17 +24119,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) {
return undefined;
const cacheKey = source.id + "," + target.id + "," + constraint.id;
if (reverseMappedCache.has(cacheKey)) {
return reverseMappedCache.get(cacheKey);
}
const key = source.id + "," + target.id + "," + constraint.id;
if (reverseMappedCache.has(key)) {
return reverseMappedCache.get(key);
const recursionKey = source.id + "," + (target.target || target).id;
if (contains(homomorphicMappedTypeInferenceStack, recursionKey)) {
return undefined;
}
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;
}

Expand Down
26 changes: 20 additions & 6 deletions tests/baselines/reference/mappedTypeRecursiveInference.errors.txt

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions tests/baselines/reference/mappedTypeRecursiveInference.types

Large diffs are not rendered by default.

73 changes: 73 additions & 0 deletions tests/baselines/reference/mappedTypeRecursiveInference2.symbols
Original file line number Diff line number Diff line change
@@ -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, --, --))

68 changes: 68 additions & 0 deletions tests/baselines/reference/mappedTypeRecursiveInference2.types
Original file line number Diff line number Diff line change
@@ -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

20 changes: 20 additions & 0 deletions tests/cases/compiler/mappedTypeRecursiveInference2.ts
Original file line number Diff line number Diff line change
@@ -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]])