Use symbols of type aliases when emitting declarations (#56087)

This commit is contained in:
Mateusz Burzyński 2024-01-09 22:02:52 +01:00 committed by GitHub
parent 4557e34e70
commit 4bcbc16cff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 172 additions and 0 deletions

View File

@ -5719,6 +5719,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
* Checks if two symbols, through aliasing and/or merging, refer to the same thing
*/
function getSymbolIfSameReference(s1: Symbol, s2: Symbol) {
if (s1.flags & SymbolFlags.TypeAlias && s2.declarations?.find(isTypeAlias)) {
s2 = getDeclaredTypeOfTypeAlias(s2).aliasSymbol || s2;
}
if (s2.flags & SymbolFlags.TypeAlias && s1.declarations?.find(isTypeAlias)) {
s1 = getDeclaredTypeOfTypeAlias(s1).aliasSymbol || s1;
}
if (getMergedSymbol(resolveSymbol(getMergedSymbol(s1))) === getMergedSymbol(resolveSymbol(getMergedSymbol(s2)))) {
return s1;
}

View File

@ -0,0 +1,47 @@
//// [tests/cases/compiler/declarationEmitUsingTypeAlias1.ts] ////
//// [inner.d.ts]
export declare type Other = { other: string };
export declare type SomeType = { arg: Other };
//// [index.d.ts]
export type OtherType = import('./inner').Other;
export type SomeType = import('./inner').SomeType;
//// [package.json]
{
"name": "some-dep",
"exports": {
".": "./dist/index.js"
}
}
//// [index.ts]
import { SomeType } from "some-dep";
export const foo = (thing: SomeType) => {
return thing;
};
export const bar = (thing: SomeType) => {
return thing.arg;
};
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bar = exports.foo = void 0;
var foo = function (thing) {
return thing;
};
exports.foo = foo;
var bar = function (thing) {
return thing.arg;
};
exports.bar = bar;
//// [index.d.ts]
import { SomeType } from "some-dep";
export declare const foo: (thing: SomeType) => import("some-dep").SomeType;
export declare const bar: (thing: SomeType) => import("some-dep").OtherType;

View File

@ -0,0 +1,46 @@
//// [tests/cases/compiler/declarationEmitUsingTypeAlias1.ts] ////
=== node_modules/some-dep/dist/inner.d.ts ===
export declare type Other = { other: string };
>Other : Symbol(Other, Decl(inner.d.ts, 0, 0))
>other : Symbol(other, Decl(inner.d.ts, 0, 29))
export declare type SomeType = { arg: Other };
>SomeType : Symbol(SomeType, Decl(inner.d.ts, 0, 46))
>arg : Symbol(arg, Decl(inner.d.ts, 1, 32))
>Other : Symbol(Other, Decl(inner.d.ts, 0, 0))
=== node_modules/some-dep/dist/index.d.ts ===
export type OtherType = import('./inner').Other;
>OtherType : Symbol(OtherType, Decl(index.d.ts, 0, 0))
>Other : Symbol(Other, Decl(inner.d.ts, 0, 0))
export type SomeType = import('./inner').SomeType;
>SomeType : Symbol(SomeType, Decl(index.d.ts, 0, 48))
>SomeType : Symbol(SomeType, Decl(inner.d.ts, 0, 46))
=== src/index.ts ===
import { SomeType } from "some-dep";
>SomeType : Symbol(SomeType, Decl(index.ts, 0, 8))
export const foo = (thing: SomeType) => {
>foo : Symbol(foo, Decl(index.ts, 2, 12))
>thing : Symbol(thing, Decl(index.ts, 2, 20))
>SomeType : Symbol(SomeType, Decl(index.ts, 0, 8))
return thing;
>thing : Symbol(thing, Decl(index.ts, 2, 20))
};
export const bar = (thing: SomeType) => {
>bar : Symbol(bar, Decl(index.ts, 6, 12))
>thing : Symbol(thing, Decl(index.ts, 6, 20))
>SomeType : Symbol(SomeType, Decl(index.ts, 0, 8))
return thing.arg;
>thing.arg : Symbol(arg, Decl(inner.d.ts, 1, 32))
>thing : Symbol(thing, Decl(index.ts, 6, 20))
>arg : Symbol(arg, Decl(inner.d.ts, 1, 32))
};

View File

@ -0,0 +1,43 @@
//// [tests/cases/compiler/declarationEmitUsingTypeAlias1.ts] ////
=== node_modules/some-dep/dist/inner.d.ts ===
export declare type Other = { other: string };
>Other : { other: string; }
>other : string
export declare type SomeType = { arg: Other };
>SomeType : { arg: Other; }
>arg : Other
=== node_modules/some-dep/dist/index.d.ts ===
export type OtherType = import('./inner').Other;
>OtherType : import("node_modules/some-dep/dist/inner").Other
export type SomeType = import('./inner').SomeType;
>SomeType : import("node_modules/some-dep/dist/inner").SomeType
=== src/index.ts ===
import { SomeType } from "some-dep";
>SomeType : any
export const foo = (thing: SomeType) => {
>foo : (thing: SomeType) => import("node_modules/some-dep/dist/inner").SomeType
>(thing: SomeType) => { return thing;} : (thing: SomeType) => import("node_modules/some-dep/dist/inner").SomeType
>thing : import("node_modules/some-dep/dist/inner").SomeType
return thing;
>thing : import("node_modules/some-dep/dist/inner").SomeType
};
export const bar = (thing: SomeType) => {
>bar : (thing: SomeType) => import("node_modules/some-dep/dist/inner").Other
>(thing: SomeType) => { return thing.arg;} : (thing: SomeType) => import("node_modules/some-dep/dist/inner").Other
>thing : import("node_modules/some-dep/dist/inner").SomeType
return thing.arg;
>thing.arg : import("node_modules/some-dep/dist/inner").Other
>thing : import("node_modules/some-dep/dist/inner").SomeType
>arg : import("node_modules/some-dep/dist/inner").Other
};

View File

@ -0,0 +1,30 @@
// @strict: true
// @declaration: true
// @module: nodenext
// @filename: node_modules/some-dep/dist/inner.d.ts
export declare type Other = { other: string };
export declare type SomeType = { arg: Other };
// @filename: node_modules/some-dep/dist/index.d.ts
export type OtherType = import('./inner').Other;
export type SomeType = import('./inner').SomeType;
// @filename: node_modules/some-dep/package.json
{
"name": "some-dep",
"exports": {
".": "./dist/index.js"
}
}
// @filename: src/index.ts
import { SomeType } from "some-dep";
export const foo = (thing: SomeType) => {
return thing;
};
export const bar = (thing: SomeType) => {
return thing.arg;
};