Exclude identity relation from mapped type relation check (#46632)

* Exclude identity relation from mapped type relation check

* Add regression test
This commit is contained in:
Anders Hejlsberg 2021-11-02 06:54:35 -07:00 committed by GitHub
parent 2424d0e575
commit 7cbcfeea99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 141 additions and 1 deletions

View file

@ -19046,7 +19046,7 @@ namespace ts {
originalErrorInfo = undefined;
}
}
else if (isGenericMappedType(target)) {
else if (isGenericMappedType(target) && relation !== identityRelation) {
// Check if source type `S` is related to target type `{ [P in Q]: T }` or `{ [P in Q as R]: T}`.
const keysRemapped = !!target.declaration.nameType;
const templateType = getTemplateTypeFromMappedType(target);

View file

@ -0,0 +1,22 @@
tests/cases/compiler/noExcessiveStackDepthError.ts(13,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'FindConditions<any>', but here has type 'FindConditions<Entity>'.
==== tests/cases/compiler/noExcessiveStackDepthError.ts (1 errors) ====
// Repro from #46631
interface FindOperator<T> {
foo: T;
}
type FindConditions<T> = {
[P in keyof T]?: FindConditions<T[P]> | FindOperator<FindConditions<T[P]>>;
};
function foo<Entity>() {
var x: FindConditions<any>;
var x: FindConditions<Entity>; // Excessive stack depth error not expected here
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'FindConditions<any>', but here has type 'FindConditions<Entity>'.
!!! related TS6203 tests/cases/compiler/noExcessiveStackDepthError.ts:12:9: 'x' was also declared here.
}

View file

@ -0,0 +1,34 @@
//// [noExcessiveStackDepthError.ts]
// Repro from #46631
interface FindOperator<T> {
foo: T;
}
type FindConditions<T> = {
[P in keyof T]?: FindConditions<T[P]> | FindOperator<FindConditions<T[P]>>;
};
function foo<Entity>() {
var x: FindConditions<any>;
var x: FindConditions<Entity>; // Excessive stack depth error not expected here
}
//// [noExcessiveStackDepthError.js]
"use strict";
// Repro from #46631
function foo() {
var x;
var x; // Excessive stack depth error not expected here
}
//// [noExcessiveStackDepthError.d.ts]
interface FindOperator<T> {
foo: T;
}
declare type FindConditions<T> = {
[P in keyof T]?: FindConditions<T[P]> | FindOperator<FindConditions<T[P]>>;
};
declare function foo<Entity>(): void;

View file

@ -0,0 +1,43 @@
=== tests/cases/compiler/noExcessiveStackDepthError.ts ===
// Repro from #46631
interface FindOperator<T> {
>FindOperator : Symbol(FindOperator, Decl(noExcessiveStackDepthError.ts, 0, 0))
>T : Symbol(T, Decl(noExcessiveStackDepthError.ts, 2, 23))
foo: T;
>foo : Symbol(FindOperator.foo, Decl(noExcessiveStackDepthError.ts, 2, 27))
>T : Symbol(T, Decl(noExcessiveStackDepthError.ts, 2, 23))
}
type FindConditions<T> = {
>FindConditions : Symbol(FindConditions, Decl(noExcessiveStackDepthError.ts, 4, 1))
>T : Symbol(T, Decl(noExcessiveStackDepthError.ts, 6, 20))
[P in keyof T]?: FindConditions<T[P]> | FindOperator<FindConditions<T[P]>>;
>P : Symbol(P, Decl(noExcessiveStackDepthError.ts, 7, 5))
>T : Symbol(T, Decl(noExcessiveStackDepthError.ts, 6, 20))
>FindConditions : Symbol(FindConditions, Decl(noExcessiveStackDepthError.ts, 4, 1))
>T : Symbol(T, Decl(noExcessiveStackDepthError.ts, 6, 20))
>P : Symbol(P, Decl(noExcessiveStackDepthError.ts, 7, 5))
>FindOperator : Symbol(FindOperator, Decl(noExcessiveStackDepthError.ts, 0, 0))
>FindConditions : Symbol(FindConditions, Decl(noExcessiveStackDepthError.ts, 4, 1))
>T : Symbol(T, Decl(noExcessiveStackDepthError.ts, 6, 20))
>P : Symbol(P, Decl(noExcessiveStackDepthError.ts, 7, 5))
};
function foo<Entity>() {
>foo : Symbol(foo, Decl(noExcessiveStackDepthError.ts, 8, 2))
>Entity : Symbol(Entity, Decl(noExcessiveStackDepthError.ts, 10, 13))
var x: FindConditions<any>;
>x : Symbol(x, Decl(noExcessiveStackDepthError.ts, 11, 7), Decl(noExcessiveStackDepthError.ts, 12, 7))
>FindConditions : Symbol(FindConditions, Decl(noExcessiveStackDepthError.ts, 4, 1))
var x: FindConditions<Entity>; // Excessive stack depth error not expected here
>x : Symbol(x, Decl(noExcessiveStackDepthError.ts, 11, 7), Decl(noExcessiveStackDepthError.ts, 12, 7))
>FindConditions : Symbol(FindConditions, Decl(noExcessiveStackDepthError.ts, 4, 1))
>Entity : Symbol(Entity, Decl(noExcessiveStackDepthError.ts, 10, 13))
}

View file

@ -0,0 +1,24 @@
=== tests/cases/compiler/noExcessiveStackDepthError.ts ===
// Repro from #46631
interface FindOperator<T> {
foo: T;
>foo : T
}
type FindConditions<T> = {
>FindConditions : FindConditions<T>
[P in keyof T]?: FindConditions<T[P]> | FindOperator<FindConditions<T[P]>>;
};
function foo<Entity>() {
>foo : <Entity>() => void
var x: FindConditions<any>;
>x : FindConditions<any>
var x: FindConditions<Entity>; // Excessive stack depth error not expected here
>x : FindConditions<any>
}

View file

@ -0,0 +1,17 @@
// @strict: true
// @declaration: true
// Repro from #46631
interface FindOperator<T> {
foo: T;
}
type FindConditions<T> = {
[P in keyof T]?: FindConditions<T[P]> | FindOperator<FindConditions<T[P]>>;
};
function foo<Entity>() {
var x: FindConditions<any>;
var x: FindConditions<Entity>; // Excessive stack depth error not expected here
}