Skip to content

Commit 565006c

Browse files
authored
Adjust the node builder compatible reference check to handle aliased tuples (microsoft#58066)
1 parent 82897a0 commit 565006c

24 files changed

+57
-48
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8561,7 +8561,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
85618561
}
85628562

85638563
function existingTypeNodeIsNotReferenceOrIsReferenceWithCompatibleTypeArgumentCount(existing: TypeNode, type: Type) {
8564-
return !(getObjectFlags(type) & ObjectFlags.Reference) || !isTypeReferenceNode(existing) || length(existing.typeArguments) >= getMinTypeArgumentCount((type as TypeReference).target.typeParameters);
8564+
// In JS, you can say something like `Foo` and get a `Foo<any>` implicitly - we don't want to preserve that original `Foo` in these cases, though.
8565+
if (!(getObjectFlags(type) & ObjectFlags.Reference)) return true;
8566+
if (!isTypeReferenceNode(existing)) return true;
8567+
// `type` is a reference type, and `existing` is a type reference node, but we still need to make sure they refer to the _same_ target type
8568+
// before we go comparing their type argument counts.
8569+
void getTypeFromTypeReference(existing); // call to ensure symbol is resolved
8570+
const symbol = getNodeLinks(existing).resolvedSymbol;
8571+
const existingTarget = symbol && getDeclaredTypeOfSymbol(symbol);
8572+
if (!existingTarget || existingTarget !== (type as TypeReference).target) return true;
8573+
return length(existing.typeArguments) >= getMinTypeArgumentCount((type as TypeReference).target.typeParameters);
85658574
}
85668575

85678576
function getEnclosingDeclarationIgnoringFakeScope(enclosingDeclaration: Node) {

tests/baselines/reference/contextualTypeTupleEnd.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ declare function str(x: string): void;
2323

2424
declare function f1(...args: Funcs): void;
2525
>f1 : (...args: Funcs) => void
26-
> : ^^^^^^^^^^^^^^^^^^^^
26+
> : ^^^^^^^^^^ ^^^^^
2727
>args : Funcs
2828
> : ^^^^^
2929

tests/baselines/reference/destructuringParameterDeclaration3ES5.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ function a3(...a: Array<String>) { }
3939

4040
function a4(...a: arrayString) { }
4141
>a4 : (...a: arrayString) => void
42-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
> : ^^^^^^^ ^^^^^^^^^
4343
>a : arrayString
4444
> : ^^^^^^^^^^^
4545

4646
function a5(...a: stringOrNumArray) { }
4747
>a5 : (...a: stringOrNumArray) => void
48-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
> : ^^^^^^^ ^^^^^^^^^
4949
>a : stringOrNumArray
5050
> : ^^^^^^^^^^^^^^^^
5151

tests/baselines/reference/destructuringParameterDeclaration3ES5iterable.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ function a3(...a: Array<String>) { }
3939

4040
function a4(...a: arrayString) { }
4141
>a4 : (...a: arrayString) => void
42-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
> : ^^^^^^^ ^^^^^^^^^
4343
>a : arrayString
4444
> : ^^^^^^^^^^^
4545

4646
function a5(...a: stringOrNumArray) { }
4747
>a5 : (...a: stringOrNumArray) => void
48-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
> : ^^^^^^^ ^^^^^^^^^
4949
>a : stringOrNumArray
5050
> : ^^^^^^^^^^^^^^^^
5151

tests/baselines/reference/destructuringParameterDeclaration3ES6.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ function a3(...a: Array<String>) { }
3939

4040
function a4(...a: arrayString) { }
4141
>a4 : (...a: arrayString) => void
42-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
> : ^^^^^^^ ^^^^^^^^^
4343
>a : arrayString
4444
> : ^^^^^^^^^^^
4545

4646
function a5(...a: stringOrNumArray) { }
4747
>a5 : (...a: stringOrNumArray) => void
48-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
> : ^^^^^^^ ^^^^^^^^^
4949
>a : stringOrNumArray
5050
> : ^^^^^^^^^^^^^^^^
5151

tests/baselines/reference/directDependenceBetweenTypeAliases.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ type T13 = typeof zz
113113

114114
var zz: { x: T11 }
115115
>zz : { x: T11; }
116-
> : ^^^^^^^^^^^
116+
> : ^^^^^ ^^^
117117
>x : T11
118118
> : ^^^
119119

tests/baselines/reference/inferFromNestedSameShapeTuple.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ type T2<T> = [42, T2<{ x: T }>];
143143

144144
function qq<U>(x: T1<U>, y: T2<U>) {
145145
>qq : <U>(x: T1<U>, y: T2<U>) => void
146-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
146+
> : ^ ^^^^^ ^^^^^ ^^^^^^^^^
147147
>x : T1<U>
148148
> : ^^^^^
149149
>y : T2<U>

tests/baselines/reference/jsdocTemplateTagDefault.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ declare function f3<T = U, U = T>(a: T, b: U): void;
176176
* @template {string | number} [T=string] - ok: defaults are permitted
177177
* @typedef {[T]} A
178178
*/
179-
/** @type {A} */ declare const aDefault1: A<string>;
180-
/** @type {A} */ declare const aDefault2: A<string>;
179+
/** @type {A} */ declare const aDefault1: A;
180+
/** @type {A} */ declare const aDefault2: A;
181181
/** @type {A<string>} */ declare const aString: A<string>;
182182
/** @type {A<number>} */ declare const aNumber: A<number>;
183183
type B<T, U = T> = [T, U];

tests/baselines/reference/mapOnTupleTypes02.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export type Point = [number, number];
77

88
export function increment(point: Point) {
99
>increment : (point: Point) => number[]
10-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
> : ^^^^^^^^ ^^^^^^^^^^^^^
1111
>point : Point
1212
> : ^^^^^
1313

tests/baselines/reference/objectTypeWithStringAndNumberIndexSignatureToAny.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ interface StringToAnyNumberToNumber extends StringTo<any>, NumberToNumber {
272272

273273
function f3(sToAny: StringTo<any>, nToNumber: NumberToNumber, strToAnyNumToNum: StringToAnyNumberToNumber, someObj: Obj) {
274274
>f3 : (sToAny: StringTo<any>, nToNumber: NumberToNumber, strToAnyNumToNum: StringToAnyNumberToNumber, someObj: Obj) => void
275-
> : ^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^
275+
> : ^^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^
276276
>sToAny : StringTo<any>
277277
> : ^^^^^^^^^^^^^
278278
>nToNumber : NumberToNumber

0 commit comments

Comments
 (0)