mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 07:45:18 -06:00
Normalize generic tuple types with Simplifiable elements (#52385)
This commit is contained in:
parent
a5e6dee6b2
commit
56aa3d522b
@ -20126,6 +20126,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
function getNormalizedType(type: Type, writing: boolean): Type {
|
||||
while (true) {
|
||||
const t = isFreshLiteralType(type) ? (type as FreshableType).regularType :
|
||||
isGenericTupleType(type) ? getNormalizedTupleType(type, writing) :
|
||||
getObjectFlags(type) & ObjectFlags.Reference ? (type as TypeReference).node ? createTypeReference((type as TypeReference).target, getTypeArguments(type as TypeReference)) : getSingleBaseForNonAugmentingSubtype(type) || type :
|
||||
type.flags & TypeFlags.UnionOrIntersection ? getNormalizedUnionOrIntersectionType(type as UnionOrIntersectionType, writing) :
|
||||
type.flags & TypeFlags.Substitution ? writing ? (type as SubstitutionType).baseType : getSubstitutionIntersection(type as SubstitutionType) :
|
||||
@ -20150,6 +20151,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
return type;
|
||||
}
|
||||
|
||||
function getNormalizedTupleType(type: TupleTypeReference, writing: boolean): Type {
|
||||
const elements = getTypeArguments(type);
|
||||
const normalizedElements = sameMap(elements, t => t.flags & TypeFlags.Simplifiable ? getSimplifiedType(t, writing) : t);
|
||||
return elements !== normalizedElements ? createNormalizedTupleType(type.target, normalizedElements) : type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if 'source' is related to 'target' (e.g.: is a assignable to).
|
||||
* @param source The left-hand-side of the relation.
|
||||
|
||||
@ -0,0 +1,42 @@
|
||||
=== tests/cases/compiler/genericTupleWithSimplifiableElements.ts ===
|
||||
// repro from #52354
|
||||
|
||||
type SS1 = string;
|
||||
>SS1 : Symbol(SS1, Decl(genericTupleWithSimplifiableElements.ts, 0, 0))
|
||||
|
||||
let y: [t: "AAA", ...args: { [S in SS1]: [a: number]; }[SS1]] = ["AAA", 1];
|
||||
>y : Symbol(y, Decl(genericTupleWithSimplifiableElements.ts, 3, 3))
|
||||
>S : Symbol(S, Decl(genericTupleWithSimplifiableElements.ts, 3, 30))
|
||||
>SS1 : Symbol(SS1, Decl(genericTupleWithSimplifiableElements.ts, 0, 0))
|
||||
>SS1 : Symbol(SS1, Decl(genericTupleWithSimplifiableElements.ts, 0, 0))
|
||||
|
||||
type SS2 = "1" | "2" | "3";
|
||||
>SS2 : Symbol(SS2, Decl(genericTupleWithSimplifiableElements.ts, 3, 75))
|
||||
|
||||
let z: [t: "AAA", ...args: { [S in SS2]: [a: number]; }[SS2]] = ["AAA", 1];
|
||||
>z : Symbol(z, Decl(genericTupleWithSimplifiableElements.ts, 6, 3))
|
||||
>S : Symbol(S, Decl(genericTupleWithSimplifiableElements.ts, 6, 30))
|
||||
>SS2 : Symbol(SS2, Decl(genericTupleWithSimplifiableElements.ts, 3, 75))
|
||||
>SS2 : Symbol(SS2, Decl(genericTupleWithSimplifiableElements.ts, 3, 75))
|
||||
|
||||
class I<SS extends string>{
|
||||
>I : Symbol(I, Decl(genericTupleWithSimplifiableElements.ts, 6, 75))
|
||||
>SS : Symbol(SS, Decl(genericTupleWithSimplifiableElements.ts, 8, 8))
|
||||
|
||||
f() {
|
||||
>f : Symbol(I.f, Decl(genericTupleWithSimplifiableElements.ts, 8, 27))
|
||||
|
||||
let w: [...args: { [S in SS]: [a: number]; }[SS]] = [1];
|
||||
>w : Symbol(w, Decl(genericTupleWithSimplifiableElements.ts, 10, 11))
|
||||
>S : Symbol(S, Decl(genericTupleWithSimplifiableElements.ts, 10, 28))
|
||||
>SS : Symbol(SS, Decl(genericTupleWithSimplifiableElements.ts, 8, 8))
|
||||
>SS : Symbol(SS, Decl(genericTupleWithSimplifiableElements.ts, 8, 8))
|
||||
|
||||
let x: [t: "AAA", ...args: { [S in SS]: [a: number]; }[SS]] = ["AAA", 1];
|
||||
>x : Symbol(x, Decl(genericTupleWithSimplifiableElements.ts, 12, 11))
|
||||
>S : Symbol(S, Decl(genericTupleWithSimplifiableElements.ts, 12, 38))
|
||||
>SS : Symbol(SS, Decl(genericTupleWithSimplifiableElements.ts, 8, 8))
|
||||
>SS : Symbol(SS, Decl(genericTupleWithSimplifiableElements.ts, 8, 8))
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,40 @@
|
||||
=== tests/cases/compiler/genericTupleWithSimplifiableElements.ts ===
|
||||
// repro from #52354
|
||||
|
||||
type SS1 = string;
|
||||
>SS1 : string
|
||||
|
||||
let y: [t: "AAA", ...args: { [S in SS1]: [a: number]; }[SS1]] = ["AAA", 1];
|
||||
>y : [t: "AAA", a: number]
|
||||
>["AAA", 1] : ["AAA", number]
|
||||
>"AAA" : "AAA"
|
||||
>1 : 1
|
||||
|
||||
type SS2 = "1" | "2" | "3";
|
||||
>SS2 : "1" | "2" | "3"
|
||||
|
||||
let z: [t: "AAA", ...args: { [S in SS2]: [a: number]; }[SS2]] = ["AAA", 1];
|
||||
>z : [t: "AAA", a: number]
|
||||
>["AAA", 1] : ["AAA", number]
|
||||
>"AAA" : "AAA"
|
||||
>1 : 1
|
||||
|
||||
class I<SS extends string>{
|
||||
>I : I<SS>
|
||||
|
||||
f() {
|
||||
>f : () => void
|
||||
|
||||
let w: [...args: { [S in SS]: [a: number]; }[SS]] = [1];
|
||||
>w : [...args: { [S in SS]: [a: number]; }[SS]]
|
||||
>[1] : [1]
|
||||
>1 : 1
|
||||
|
||||
let x: [t: "AAA", ...args: { [S in SS]: [a: number]; }[SS]] = ["AAA", 1];
|
||||
>x : [t: "AAA", ...args: { [S in SS]: [a: number]; }[SS]]
|
||||
>["AAA", 1] : ["AAA", 1]
|
||||
>"AAA" : "AAA"
|
||||
>1 : 1
|
||||
}
|
||||
}
|
||||
|
||||
18
tests/cases/compiler/genericTupleWithSimplifiableElements.ts
Normal file
18
tests/cases/compiler/genericTupleWithSimplifiableElements.ts
Normal file
@ -0,0 +1,18 @@
|
||||
// @strict: true
|
||||
// @noEmit: true
|
||||
|
||||
// repro from #52354
|
||||
|
||||
type SS1 = string;
|
||||
let y: [t: "AAA", ...args: { [S in SS1]: [a: number]; }[SS1]] = ["AAA", 1];
|
||||
|
||||
type SS2 = "1" | "2" | "3";
|
||||
let z: [t: "AAA", ...args: { [S in SS2]: [a: number]; }[SS2]] = ["AAA", 1];
|
||||
|
||||
class I<SS extends string>{
|
||||
f() {
|
||||
let w: [...args: { [S in SS]: [a: number]; }[SS]] = [1];
|
||||
|
||||
let x: [t: "AAA", ...args: { [S in SS]: [a: number]; }[SS]] = ["AAA", 1];
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user