Skip to content
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

Use symbols of type aliases when emitting declarations #56087

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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: 6 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5665,6 +5665,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (getSymbolIfSameReference(exported, symbol)) {
return exported;
}
if (symbol.flags & SymbolFlags.TypeAlias && exported.declarations?.find(isTypeAlias)) {
Copy link
Member

Choose a reason for hiding this comment

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

I think we canonically use our own find helper here to avoid the this penalty; might be important if this is hot code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have literally copied this bit from getDeclaredTypeOfTypeAlias :P
https://github.dev/microsoft/TypeScript/blob/cf8763b6c3193deefd095c86ce79921f23de307b/src/compiler/checker.ts#L12436

Should I switch both call sites to ur custom find?

Copy link
Member

Choose a reason for hiding this comment

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

Hah, then nevermind.

Copy link
Member

@sandersn sandersn Nov 3, 2023

Choose a reason for hiding this comment

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

to avoid the this penalty

The original reason was that find wasn't defined in old runtimes. What is the this penalty? Maybe that's a good replacement reason.

Copy link
Member

Choose a reason for hiding this comment

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

IIRC functions passed to find and other helpers like it are bound such that this in them points to the array.

const aliasSymbol = getDeclaredTypeOfTypeAlias(exported).aliasSymbol;
if (aliasSymbol && getSymbolIfSameReference(aliasSymbol, symbol)) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be a change in the logic of getSymbolIfSameReference instead? To make a type alias that points directly at another type count as a "same reference" of that type? It's been reused in one or two other places, but it basically only exists to answer "should we consider these two symbols as ultimately referring to the same thing", and mostly just for the usages here, in this function. In particular, only doing this check here means 1. the fastpath lookup will fail for type aliases that pass this check, so they always hit the slow path and 2. an export ='d type alias will never be considered equal to the target symbol.

return exported;
}
}
});
}
Copy link
Member

Choose a reason for hiding this comment

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

Maybe extract this into another helper function? Seems a bit repetitive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This checks both s1 and s2 and I don't have any good name for a helper function that would accept both as arguments or other ideas on how to improve this. Everything I have tried locally felt clunky to me.


Expand Down
47 changes: 47 additions & 0 deletions tests/baselines/reference/declarationEmitUsingTypeAlias1.js
Original file line number Diff line number Diff line change
@@ -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;
46 changes: 46 additions & 0 deletions tests/baselines/reference/declarationEmitUsingTypeAlias1.symbols
Original file line number Diff line number Diff line change
@@ -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))

};
43 changes: 43 additions & 0 deletions tests/baselines/reference/declarationEmitUsingTypeAlias1.types
Original file line number Diff line number Diff line change
@@ -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

};
30 changes: 30 additions & 0 deletions tests/cases/compiler/declarationEmitUsingTypeAlias1.ts
Original file line number Diff line number Diff line change
@@ -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;
};
Loading