Skip to content

Commit a34ac80

Browse files
authored
Walk up parenthesized type nodes when looking for the type alias hosting a node (#32924)
1 parent 24201cf commit a34ac80

7 files changed

+175
-3
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10894,7 +10894,11 @@ namespace ts {
1089410894
}
1089510895

1089610896
function getAliasSymbolForTypeNode(node: TypeNode) {
10897-
return isTypeAlias(node.parent) ? getSymbolOfNode(node.parent) : undefined;
10897+
let host = node.parent;
10898+
while (isParenthesizedTypeNode(host)) {
10899+
host = host.parent;
10900+
}
10901+
return isTypeAlias(host) ? getSymbolOfNode(host) : undefined;
1089810902
}
1089910903

1090010904
function getTypeArgumentsForAliasSymbol(symbol: Symbol | undefined) {

tests/baselines/reference/destructuringInFunctionType.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type F1 = ([a, b, c]) => void;
1818
>c : any
1919

2020
type T2 = ({ a });
21-
>T2 : { a: any; }
21+
>T2 : T2
2222
>a : any
2323

2424
type F2 = ({ a }) => void;

tests/baselines/reference/keyofIntersection.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type Result3 = Example3<'x' | 'y'>; // "x" | "y"
4646
>Result3 : "x" | "y"
4747

4848
type Example4<T extends string, U extends string> = (Record<T, any> & Record<U, any>);
49-
>Example4 : Record<T, any> & Record<U, any>
49+
>Example4 : Example4<T, U>
5050

5151
type Result4 = keyof Example4<'x', 'y'>; // "x" | "y"
5252
>Result4 : "x" | "y"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//// [parenthesisDoesNotBlockAliasSymbolCreation.ts]
2+
export type InvalidKeys<K extends string|number|symbol> = { [P in K]? : never };
3+
export type InvalidKeys2<K extends string|number|symbol> = (
4+
{ [P in K]? : never }
5+
);
6+
7+
export type A<T> = (
8+
T & InvalidKeys<"a">
9+
);
10+
export type A2<T> = (
11+
T & InvalidKeys2<"a">
12+
);
13+
14+
export const a = null as A<{ x : number }>;
15+
export const a2 = null as A2<{ x : number }>;
16+
export const a3 = null as { x : number } & InvalidKeys<"a">;
17+
export const a4 = null as { x : number } & InvalidKeys2<"a">;
18+
19+
20+
//// [parenthesisDoesNotBlockAliasSymbolCreation.js]
21+
"use strict";
22+
exports.__esModule = true;
23+
exports.a = null;
24+
exports.a2 = null;
25+
exports.a3 = null;
26+
exports.a4 = null;
27+
28+
29+
//// [parenthesisDoesNotBlockAliasSymbolCreation.d.ts]
30+
export declare type InvalidKeys<K extends string | number | symbol> = {
31+
[P in K]?: never;
32+
};
33+
export declare type InvalidKeys2<K extends string | number | symbol> = ({
34+
[P in K]?: never;
35+
});
36+
export declare type A<T> = (T & InvalidKeys<"a">);
37+
export declare type A2<T> = (T & InvalidKeys2<"a">);
38+
export declare const a: A<{
39+
x: number;
40+
}>;
41+
export declare const a2: A2<{
42+
x: number;
43+
}>;
44+
export declare const a3: {
45+
x: number;
46+
} & InvalidKeys<"a">;
47+
export declare const a4: {
48+
x: number;
49+
} & InvalidKeys2<"a">;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
=== tests/cases/compiler/parenthesisDoesNotBlockAliasSymbolCreation.ts ===
2+
export type InvalidKeys<K extends string|number|symbol> = { [P in K]? : never };
3+
>InvalidKeys : Symbol(InvalidKeys, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 0))
4+
>K : Symbol(K, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 24))
5+
>P : Symbol(P, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 61))
6+
>K : Symbol(K, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 24))
7+
8+
export type InvalidKeys2<K extends string|number|symbol> = (
9+
>InvalidKeys2 : Symbol(InvalidKeys2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 80))
10+
>K : Symbol(K, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 1, 25))
11+
12+
{ [P in K]? : never }
13+
>P : Symbol(P, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 2, 7))
14+
>K : Symbol(K, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 1, 25))
15+
16+
);
17+
18+
export type A<T> = (
19+
>A : Symbol(A, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 3, 2))
20+
>T : Symbol(T, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 5, 14))
21+
22+
T & InvalidKeys<"a">
23+
>T : Symbol(T, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 5, 14))
24+
>InvalidKeys : Symbol(InvalidKeys, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 0))
25+
26+
);
27+
export type A2<T> = (
28+
>A2 : Symbol(A2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 7, 2))
29+
>T : Symbol(T, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 8, 15))
30+
31+
T & InvalidKeys2<"a">
32+
>T : Symbol(T, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 8, 15))
33+
>InvalidKeys2 : Symbol(InvalidKeys2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 80))
34+
35+
);
36+
37+
export const a = null as A<{ x : number }>;
38+
>a : Symbol(a, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 12, 12))
39+
>A : Symbol(A, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 3, 2))
40+
>x : Symbol(x, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 12, 28))
41+
42+
export const a2 = null as A2<{ x : number }>;
43+
>a2 : Symbol(a2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 13, 12))
44+
>A2 : Symbol(A2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 7, 2))
45+
>x : Symbol(x, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 13, 30))
46+
47+
export const a3 = null as { x : number } & InvalidKeys<"a">;
48+
>a3 : Symbol(a3, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 14, 12))
49+
>x : Symbol(x, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 14, 27))
50+
>InvalidKeys : Symbol(InvalidKeys, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 0))
51+
52+
export const a4 = null as { x : number } & InvalidKeys2<"a">;
53+
>a4 : Symbol(a4, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 15, 12))
54+
>x : Symbol(x, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 15, 27))
55+
>InvalidKeys2 : Symbol(InvalidKeys2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 80))
56+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
=== tests/cases/compiler/parenthesisDoesNotBlockAliasSymbolCreation.ts ===
2+
export type InvalidKeys<K extends string|number|symbol> = { [P in K]? : never };
3+
>InvalidKeys : InvalidKeys<K>
4+
5+
export type InvalidKeys2<K extends string|number|symbol> = (
6+
>InvalidKeys2 : InvalidKeys2<K>
7+
8+
{ [P in K]? : never }
9+
);
10+
11+
export type A<T> = (
12+
>A : A<T>
13+
14+
T & InvalidKeys<"a">
15+
);
16+
export type A2<T> = (
17+
>A2 : A2<T>
18+
19+
T & InvalidKeys2<"a">
20+
);
21+
22+
export const a = null as A<{ x : number }>;
23+
>a : A<{ x: number; }>
24+
>null as A<{ x : number }> : A<{ x: number; }>
25+
>null : null
26+
>x : number
27+
28+
export const a2 = null as A2<{ x : number }>;
29+
>a2 : A2<{ x: number; }>
30+
>null as A2<{ x : number }> : A2<{ x: number; }>
31+
>null : null
32+
>x : number
33+
34+
export const a3 = null as { x : number } & InvalidKeys<"a">;
35+
>a3 : { x: number; } & InvalidKeys<"a">
36+
>null as { x : number } & InvalidKeys<"a"> : { x: number; } & InvalidKeys<"a">
37+
>null : null
38+
>x : number
39+
40+
export const a4 = null as { x : number } & InvalidKeys2<"a">;
41+
>a4 : { x: number; } & InvalidKeys2<"a">
42+
>null as { x : number } & InvalidKeys2<"a"> : { x: number; } & InvalidKeys2<"a">
43+
>null : null
44+
>x : number
45+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @declaration: true
2+
3+
export type InvalidKeys<K extends string|number|symbol> = { [P in K]? : never };
4+
export type InvalidKeys2<K extends string|number|symbol> = (
5+
{ [P in K]? : never }
6+
);
7+
8+
export type A<T> = (
9+
T & InvalidKeys<"a">
10+
);
11+
export type A2<T> = (
12+
T & InvalidKeys2<"a">
13+
);
14+
15+
export const a = null as A<{ x : number }>;
16+
export const a2 = null as A2<{ x : number }>;
17+
export const a3 = null as { x : number } & InvalidKeys<"a">;
18+
export const a4 = null as { x : number } & InvalidKeys2<"a">;

0 commit comments

Comments
 (0)