-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Use symbols of type aliases when emitting declarations #56087
Conversation
src/compiler/checker.ts
Outdated
@@ -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)) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hah, then nevermind.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
src/compiler/checker.ts
Outdated
@@ -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)) { | |||
const aliasSymbol = getDeclaredTypeOfTypeAlias(exported).aliasSymbol; | |||
if (aliasSymbol && getSymbolIfSameReference(aliasSymbol, symbol)) { |
There was a problem hiding this comment.
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.
@@ -5672,6 +5672,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)) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@typescript-bot test this |
Heya @weswigham, I've started to run the diff-based top-repos suite on this PR at 862603b. You can monitor the build here. Update: The results are in! |
Heya @weswigham, I've started to run the parallelized Definitely Typed test suite on this PR at 862603b. You can monitor the build here. Update: The results are in! |
Heya @weswigham, I've started to run the public perf test suite on this PR at 862603b. You can monitor the build here. Update: The results are in! |
Hey @weswigham, the results of running the DT tests are ready. |
@weswigham Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
@weswigham Here are the results of running the top-repos suite comparing Something interesting changed - please have a look. Details
|
I've sent a revert for this in #57849; this PR caused two bugs and a perf regression, so I think it's safest to back it out as it was just to fix a bug that has existed seemingly forever. |
fixes #49171