Skip to content

Commit 4bcbc16

Browse files
authored
Use symbols of type aliases when emitting declarations (#56087)
1 parent 4557e34 commit 4bcbc16

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5719,6 +5719,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
57195719
* Checks if two symbols, through aliasing and/or merging, refer to the same thing
57205720
*/
57215721
function getSymbolIfSameReference(s1: Symbol, s2: Symbol) {
5722+
if (s1.flags & SymbolFlags.TypeAlias && s2.declarations?.find(isTypeAlias)) {
5723+
s2 = getDeclaredTypeOfTypeAlias(s2).aliasSymbol || s2;
5724+
}
5725+
if (s2.flags & SymbolFlags.TypeAlias && s1.declarations?.find(isTypeAlias)) {
5726+
s1 = getDeclaredTypeOfTypeAlias(s1).aliasSymbol || s1;
5727+
}
57225728
if (getMergedSymbol(resolveSymbol(getMergedSymbol(s1))) === getMergedSymbol(resolveSymbol(getMergedSymbol(s2)))) {
57235729
return s1;
57245730
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//// [tests/cases/compiler/declarationEmitUsingTypeAlias1.ts] ////
2+
3+
//// [inner.d.ts]
4+
export declare type Other = { other: string };
5+
export declare type SomeType = { arg: Other };
6+
7+
//// [index.d.ts]
8+
export type OtherType = import('./inner').Other;
9+
export type SomeType = import('./inner').SomeType;
10+
11+
//// [package.json]
12+
{
13+
"name": "some-dep",
14+
"exports": {
15+
".": "./dist/index.js"
16+
}
17+
}
18+
19+
//// [index.ts]
20+
import { SomeType } from "some-dep";
21+
22+
export const foo = (thing: SomeType) => {
23+
return thing;
24+
};
25+
26+
export const bar = (thing: SomeType) => {
27+
return thing.arg;
28+
};
29+
30+
//// [index.js]
31+
"use strict";
32+
Object.defineProperty(exports, "__esModule", { value: true });
33+
exports.bar = exports.foo = void 0;
34+
var foo = function (thing) {
35+
return thing;
36+
};
37+
exports.foo = foo;
38+
var bar = function (thing) {
39+
return thing.arg;
40+
};
41+
exports.bar = bar;
42+
43+
44+
//// [index.d.ts]
45+
import { SomeType } from "some-dep";
46+
export declare const foo: (thing: SomeType) => import("some-dep").SomeType;
47+
export declare const bar: (thing: SomeType) => import("some-dep").OtherType;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//// [tests/cases/compiler/declarationEmitUsingTypeAlias1.ts] ////
2+
3+
=== node_modules/some-dep/dist/inner.d.ts ===
4+
export declare type Other = { other: string };
5+
>Other : Symbol(Other, Decl(inner.d.ts, 0, 0))
6+
>other : Symbol(other, Decl(inner.d.ts, 0, 29))
7+
8+
export declare type SomeType = { arg: Other };
9+
>SomeType : Symbol(SomeType, Decl(inner.d.ts, 0, 46))
10+
>arg : Symbol(arg, Decl(inner.d.ts, 1, 32))
11+
>Other : Symbol(Other, Decl(inner.d.ts, 0, 0))
12+
13+
=== node_modules/some-dep/dist/index.d.ts ===
14+
export type OtherType = import('./inner').Other;
15+
>OtherType : Symbol(OtherType, Decl(index.d.ts, 0, 0))
16+
>Other : Symbol(Other, Decl(inner.d.ts, 0, 0))
17+
18+
export type SomeType = import('./inner').SomeType;
19+
>SomeType : Symbol(SomeType, Decl(index.d.ts, 0, 48))
20+
>SomeType : Symbol(SomeType, Decl(inner.d.ts, 0, 46))
21+
22+
=== src/index.ts ===
23+
import { SomeType } from "some-dep";
24+
>SomeType : Symbol(SomeType, Decl(index.ts, 0, 8))
25+
26+
export const foo = (thing: SomeType) => {
27+
>foo : Symbol(foo, Decl(index.ts, 2, 12))
28+
>thing : Symbol(thing, Decl(index.ts, 2, 20))
29+
>SomeType : Symbol(SomeType, Decl(index.ts, 0, 8))
30+
31+
return thing;
32+
>thing : Symbol(thing, Decl(index.ts, 2, 20))
33+
34+
};
35+
36+
export const bar = (thing: SomeType) => {
37+
>bar : Symbol(bar, Decl(index.ts, 6, 12))
38+
>thing : Symbol(thing, Decl(index.ts, 6, 20))
39+
>SomeType : Symbol(SomeType, Decl(index.ts, 0, 8))
40+
41+
return thing.arg;
42+
>thing.arg : Symbol(arg, Decl(inner.d.ts, 1, 32))
43+
>thing : Symbol(thing, Decl(index.ts, 6, 20))
44+
>arg : Symbol(arg, Decl(inner.d.ts, 1, 32))
45+
46+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//// [tests/cases/compiler/declarationEmitUsingTypeAlias1.ts] ////
2+
3+
=== node_modules/some-dep/dist/inner.d.ts ===
4+
export declare type Other = { other: string };
5+
>Other : { other: string; }
6+
>other : string
7+
8+
export declare type SomeType = { arg: Other };
9+
>SomeType : { arg: Other; }
10+
>arg : Other
11+
12+
=== node_modules/some-dep/dist/index.d.ts ===
13+
export type OtherType = import('./inner').Other;
14+
>OtherType : import("node_modules/some-dep/dist/inner").Other
15+
16+
export type SomeType = import('./inner').SomeType;
17+
>SomeType : import("node_modules/some-dep/dist/inner").SomeType
18+
19+
=== src/index.ts ===
20+
import { SomeType } from "some-dep";
21+
>SomeType : any
22+
23+
export const foo = (thing: SomeType) => {
24+
>foo : (thing: SomeType) => import("node_modules/some-dep/dist/inner").SomeType
25+
>(thing: SomeType) => { return thing;} : (thing: SomeType) => import("node_modules/some-dep/dist/inner").SomeType
26+
>thing : import("node_modules/some-dep/dist/inner").SomeType
27+
28+
return thing;
29+
>thing : import("node_modules/some-dep/dist/inner").SomeType
30+
31+
};
32+
33+
export const bar = (thing: SomeType) => {
34+
>bar : (thing: SomeType) => import("node_modules/some-dep/dist/inner").Other
35+
>(thing: SomeType) => { return thing.arg;} : (thing: SomeType) => import("node_modules/some-dep/dist/inner").Other
36+
>thing : import("node_modules/some-dep/dist/inner").SomeType
37+
38+
return thing.arg;
39+
>thing.arg : import("node_modules/some-dep/dist/inner").Other
40+
>thing : import("node_modules/some-dep/dist/inner").SomeType
41+
>arg : import("node_modules/some-dep/dist/inner").Other
42+
43+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// @strict: true
2+
// @declaration: true
3+
// @module: nodenext
4+
5+
// @filename: node_modules/some-dep/dist/inner.d.ts
6+
export declare type Other = { other: string };
7+
export declare type SomeType = { arg: Other };
8+
9+
// @filename: node_modules/some-dep/dist/index.d.ts
10+
export type OtherType = import('./inner').Other;
11+
export type SomeType = import('./inner').SomeType;
12+
13+
// @filename: node_modules/some-dep/package.json
14+
{
15+
"name": "some-dep",
16+
"exports": {
17+
".": "./dist/index.js"
18+
}
19+
}
20+
21+
// @filename: src/index.ts
22+
import { SomeType } from "some-dep";
23+
24+
export const foo = (thing: SomeType) => {
25+
return thing;
26+
};
27+
28+
export const bar = (thing: SomeType) => {
29+
return thing.arg;
30+
};

0 commit comments

Comments
 (0)