Skip to content

Walk up parenthesized type nodes when looking for the type alias hosting a node #32924

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
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
6 changes: 5 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10868,7 +10868,11 @@ namespace ts {
}

function getAliasSymbolForTypeNode(node: TypeNode) {
return isTypeAlias(node.parent) ? getSymbolOfNode(node.parent) : undefined;
let host = node.parent;
while (isParenthesizedTypeNode(host)) {
host = host.parent;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

<3

return isTypeAlias(host) ? getSymbolOfNode(host) : undefined;
}

function getTypeArgumentsForAliasSymbol(symbol: Symbol | undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type F1 = ([a, b, c]) => void;
>c : any

type T2 = ({ a });
>T2 : { a: any; }
>T2 : T2
Copy link
Member

Choose a reason for hiding this comment

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

I can’t tell whether this makes sense because I can’t immediately deduce the rule for type readouts by reading the pre-existing examples below. F2 gets reported as F2 but T3 prints out the tuple literal structure.

Copy link
Member Author

Choose a reason for hiding this comment

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

Tuples are interned and don't have alias symbols, objects have alias symbols, so print as their alias if visible.

>a : any

type F2 = ({ a }) => void;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/keyofIntersection.types
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Result3 = Example3<'x' | 'y'>; // "x" | "y"
>Result3 : "x" | "y"

type Example4<T extends string, U extends string> = (Record<T, any> & Record<U, any>);
>Example4 : Record<T, any> & Record<U, any>
>Example4 : Example4<T, U>

type Result4 = keyof Example4<'x', 'y'>; // "x" | "y"
>Result4 : "x" | "y"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//// [parenthesisDoesNotBlockAliasSymbolCreation.ts]
export type InvalidKeys<K extends string|number|symbol> = { [P in K]? : never };
export type InvalidKeys2<K extends string|number|symbol> = (
{ [P in K]? : never }
);

export type A<T> = (
T & InvalidKeys<"a">
);
export type A2<T> = (
T & InvalidKeys2<"a">
);

export const a = null as A<{ x : number }>;
export const a2 = null as A2<{ x : number }>;
export const a3 = null as { x : number } & InvalidKeys<"a">;
export const a4 = null as { x : number } & InvalidKeys2<"a">;


//// [parenthesisDoesNotBlockAliasSymbolCreation.js]
"use strict";
exports.__esModule = true;
exports.a = null;
exports.a2 = null;
exports.a3 = null;
exports.a4 = null;


//// [parenthesisDoesNotBlockAliasSymbolCreation.d.ts]
export declare type InvalidKeys<K extends string | number | symbol> = {
[P in K]?: never;
};
export declare type InvalidKeys2<K extends string | number | symbol> = ({
[P in K]?: never;
});
export declare type A<T> = (T & InvalidKeys<"a">);
export declare type A2<T> = (T & InvalidKeys2<"a">);
export declare const a: A<{
x: number;
}>;
export declare const a2: A2<{
x: number;
}>;
export declare const a3: {
x: number;
} & InvalidKeys<"a">;
export declare const a4: {
x: number;
} & InvalidKeys2<"a">;
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
=== tests/cases/compiler/parenthesisDoesNotBlockAliasSymbolCreation.ts ===
export type InvalidKeys<K extends string|number|symbol> = { [P in K]? : never };
>InvalidKeys : Symbol(InvalidKeys, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 0))
>K : Symbol(K, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 24))
>P : Symbol(P, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 61))
>K : Symbol(K, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 24))

export type InvalidKeys2<K extends string|number|symbol> = (
>InvalidKeys2 : Symbol(InvalidKeys2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 80))
>K : Symbol(K, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 1, 25))

{ [P in K]? : never }
>P : Symbol(P, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 2, 7))
>K : Symbol(K, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 1, 25))

);

export type A<T> = (
>A : Symbol(A, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 3, 2))
>T : Symbol(T, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 5, 14))

T & InvalidKeys<"a">
>T : Symbol(T, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 5, 14))
>InvalidKeys : Symbol(InvalidKeys, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 0))

);
export type A2<T> = (
>A2 : Symbol(A2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 7, 2))
>T : Symbol(T, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 8, 15))

T & InvalidKeys2<"a">
>T : Symbol(T, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 8, 15))
>InvalidKeys2 : Symbol(InvalidKeys2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 80))

);

export const a = null as A<{ x : number }>;
>a : Symbol(a, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 12, 12))
>A : Symbol(A, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 3, 2))
>x : Symbol(x, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 12, 28))

export const a2 = null as A2<{ x : number }>;
>a2 : Symbol(a2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 13, 12))
>A2 : Symbol(A2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 7, 2))
>x : Symbol(x, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 13, 30))

export const a3 = null as { x : number } & InvalidKeys<"a">;
>a3 : Symbol(a3, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 14, 12))
>x : Symbol(x, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 14, 27))
>InvalidKeys : Symbol(InvalidKeys, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 0))

export const a4 = null as { x : number } & InvalidKeys2<"a">;
>a4 : Symbol(a4, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 15, 12))
>x : Symbol(x, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 15, 27))
>InvalidKeys2 : Symbol(InvalidKeys2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 80))

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
=== tests/cases/compiler/parenthesisDoesNotBlockAliasSymbolCreation.ts ===
export type InvalidKeys<K extends string|number|symbol> = { [P in K]? : never };
>InvalidKeys : InvalidKeys<K>

export type InvalidKeys2<K extends string|number|symbol> = (
>InvalidKeys2 : InvalidKeys2<K>

{ [P in K]? : never }
);

export type A<T> = (
>A : A<T>

T & InvalidKeys<"a">
);
export type A2<T> = (
>A2 : A2<T>

T & InvalidKeys2<"a">
);

export const a = null as A<{ x : number }>;
>a : A<{ x: number; }>
>null as A<{ x : number }> : A<{ x: number; }>
>null : null
>x : number

export const a2 = null as A2<{ x : number }>;
>a2 : A2<{ x: number; }>
>null as A2<{ x : number }> : A2<{ x: number; }>
>null : null
>x : number

export const a3 = null as { x : number } & InvalidKeys<"a">;
>a3 : { x: number; } & InvalidKeys<"a">
>null as { x : number } & InvalidKeys<"a"> : { x: number; } & InvalidKeys<"a">
>null : null
>x : number

export const a4 = null as { x : number } & InvalidKeys2<"a">;
>a4 : { x: number; } & InvalidKeys2<"a">
>null as { x : number } & InvalidKeys2<"a"> : { x: number; } & InvalidKeys2<"a">
>null : null
>x : number

18 changes: 18 additions & 0 deletions tests/cases/compiler/parenthesisDoesNotBlockAliasSymbolCreation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @declaration: true

export type InvalidKeys<K extends string|number|symbol> = { [P in K]? : never };
export type InvalidKeys2<K extends string|number|symbol> = (
{ [P in K]? : never }
);

export type A<T> = (
T & InvalidKeys<"a">
);
export type A2<T> = (
T & InvalidKeys2<"a">
);

export const a = null as A<{ x : number }>;
export const a2 = null as A2<{ x : number }>;
export const a3 = null as { x : number } & InvalidKeys<"a">;
export const a4 = null as { x : number } & InvalidKeys2<"a">;