Skip to content

Fix mapped type instantiation circularity #46586

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
Oct 29, 2021
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
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16572,7 +16572,8 @@ namespace ts {
if (t.flags & (TypeFlags.AnyOrUnknown | TypeFlags.InstantiableNonPrimitive | TypeFlags.Object | TypeFlags.Intersection) && t !== wildcardType && !isErrorType(t)) {
if (!type.declaration.nameType) {
let constraint;
if (isArrayType(t) || (t.flags & TypeFlags.Any) && (constraint = getConstraintOfTypeParameter(typeVariable)) && everyType(constraint, or(isArrayType, isTupleType))) {
if (isArrayType(t) || t.flags & TypeFlags.Any && findResolutionCycleStartIndex(typeVariable, TypeSystemPropertyName.ImmediateBaseConstraint) < 0 &&
(constraint = getConstraintOfTypeParameter(typeVariable)) && everyType(constraint, or(isArrayType, isTupleType))) {
Comment on lines +16575 to +16576
Copy link
Member

@DanielRosenwasser DanielRosenwasser Oct 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One day, we should find an easy/reusable way to easily ask "is this true *assuming that there are no circularities?"

Today is not that day.

return instantiateMappedArrayType(t, type, prependTypeMapping(typeVariable, t, mapper));
}
if (isGenericTupleType(t)) {
Expand Down
12 changes: 11 additions & 1 deletion tests/baselines/reference/mappedTypeWithAny.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,14 @@ tests/cases/conformance/types/mapped/mappedTypeWithAny.ts(53,5): error TS2322: T
let def: [any, any] = stringifyPair(void 0 as any);
~~~
!!! error TS2322: Type 'string[]' is not assignable to type '[any, any]'.
!!! error TS2322: Target requires 2 element(s) but source may have fewer.
!!! error TS2322: Target requires 2 element(s) but source may have fewer.

// Repro from #46582

type Evolvable<E extends Evolver> = {
[P in keyof E]: never;
};
type Evolver<T extends Evolvable<any> = any> = {
[key in keyof Partial<T>]: never;
};

18 changes: 17 additions & 1 deletion tests/baselines/reference/mappedTypeWithAny.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ declare function stringifyArray<T extends readonly any[]>(arr: T): { -readonly [
let abc: any[] = stringifyArray(void 0 as any);

declare function stringifyPair<T extends readonly [any, any]>(arr: T): { -readonly [K in keyof T]: string };
let def: [any, any] = stringifyPair(void 0 as any);
let def: [any, any] = stringifyPair(void 0 as any);

// Repro from #46582

type Evolvable<E extends Evolver> = {
[P in keyof E]: never;
};
type Evolver<T extends Evolvable<any> = any> = {
[key in keyof Partial<T>]: never;
};


//// [mappedTypeWithAny.js]
"use strict";
Expand Down Expand Up @@ -110,3 +120,9 @@ declare function stringifyPair<T extends readonly [any, any]>(arr: T): {
-readonly [K in keyof T]: string;
};
declare let def: [any, any];
declare type Evolvable<E extends Evolver> = {
[P in keyof E]: never;
};
declare type Evolver<T extends Evolvable<any> = any> = {
[key in keyof Partial<T>]: never;
};
24 changes: 24 additions & 0 deletions tests/baselines/reference/mappedTypeWithAny.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,27 @@ let def: [any, any] = stringifyPair(void 0 as any);
>def : Symbol(def, Decl(mappedTypeWithAny.ts, 52, 3))
>stringifyPair : Symbol(stringifyPair, Decl(mappedTypeWithAny.ts, 49, 47))

// Repro from #46582

type Evolvable<E extends Evolver> = {
>Evolvable : Symbol(Evolvable, Decl(mappedTypeWithAny.ts, 52, 51))
>E : Symbol(E, Decl(mappedTypeWithAny.ts, 56, 15))
>Evolver : Symbol(Evolver, Decl(mappedTypeWithAny.ts, 58, 2))

[P in keyof E]: never;
>P : Symbol(P, Decl(mappedTypeWithAny.ts, 57, 3))
>E : Symbol(E, Decl(mappedTypeWithAny.ts, 56, 15))

};
type Evolver<T extends Evolvable<any> = any> = {
>Evolver : Symbol(Evolver, Decl(mappedTypeWithAny.ts, 58, 2))
>T : Symbol(T, Decl(mappedTypeWithAny.ts, 59, 13))
>Evolvable : Symbol(Evolvable, Decl(mappedTypeWithAny.ts, 52, 51))

[key in keyof Partial<T>]: never;
>key : Symbol(key, Decl(mappedTypeWithAny.ts, 60, 3))
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(mappedTypeWithAny.ts, 59, 13))

};

13 changes: 13 additions & 0 deletions tests/baselines/reference/mappedTypeWithAny.types
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,16 @@ let def: [any, any] = stringifyPair(void 0 as any);
>void 0 : undefined
>0 : 0

// Repro from #46582

type Evolvable<E extends Evolver> = {
>Evolvable : Evolvable<E>

[P in keyof E]: never;
};
type Evolver<T extends Evolvable<any> = any> = {
>Evolver : Evolver<T>

[key in keyof Partial<T>]: never;
};

11 changes: 10 additions & 1 deletion tests/cases/conformance/types/mapped/mappedTypeWithAny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@ declare function stringifyArray<T extends readonly any[]>(arr: T): { -readonly [
let abc: any[] = stringifyArray(void 0 as any);

declare function stringifyPair<T extends readonly [any, any]>(arr: T): { -readonly [K in keyof T]: string };
let def: [any, any] = stringifyPair(void 0 as any);
let def: [any, any] = stringifyPair(void 0 as any);

// Repro from #46582

type Evolvable<E extends Evolver> = {
[P in keyof E]: never;
};
type Evolver<T extends Evolvable<any> = any> = {
[key in keyof Partial<T>]: never;
};