-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Binding element alias is kept in function expressions even though they are never used in declaration files. #56992
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
Comments
The // index.ts
export function test1(a: number): typeof a {
return a;
}
export const test2 = function (a: number): typeof a {
return a;
};
// index.d.ts
export declare function test1(a: number): typeof a;
export declare const test2: (a: number) => number; |
It's possible that we could reuse something here, but arrow functions / function expressions are not legal initializers in declaration files and must be rewritten. Right now, it's all "consistent" and goes through the same printer. |
Thanks for the explanation - it helped me to understand how some of this is implemented :) |
Here's an interesting discrepency I found while playing around: // index.ts
export function test1(a: number): typeof a {
return a;
}
export const test2: (a: number) => typeof a = function (a: number): typeof a {
return a;
}; // index.d.ts
export declare function test1(a: number): typeof a;
export declare const test2: (a: number) => typeof a; which is consistent with what @jakebailey was saying... BUT! The hover text is different! Type hints:
Hmm... π€ (Playground link) |
@fatcerberus Type printer flags are different between declarations and tooltips probably. Declaration emit has different requirements. |
Some other cases that are not currently handled: let prop = "0";
export function fn1({ prop: a} : Prop): typeof prop {
return prop
} In this case the new parameter name export function fn2(prop: number, { prop: a }: Prop) {} In this case the new names duplicates an existing parameter name export function fn3({ prop: a }: Prop, { prop: b }: Prop) {
} It is also an issue if the duplicates are introduced by removing the alias across parameters (or could also be in nested destructurings) |
Doesn't this one prove that renames in declaration files should always be allowed (and kept)? It seems that by removing them edge cases are introduced and it's not even apparent to me how this one above could be fixed without overcomplicating things. |
I was just arriving at the same conclusion. Sure it would be nice to remove them since they are an implementation detail. But this seems to introduce a lot more problems than it's worth if we want to do it correctly. It can be done for sure, but at a non-zero cost for declaration emit performance. I would suggest we just remove this behavior an revert back to always preserving the aliases. @jakebailey @RyanCavanaugh I can open a PR to remove it you also agree with this. |
I'm fine with reverting the PR, so long as we keep all of the tests and add tests for any new edge cases. |
It's not just my last PR. It would be reverting the previous PR as well that removed aliases when they were not referenced. The duplicate identifier issue affects that implementation as well. |
Right, makes sense. |
π Search Terms
binding element alias declaration files
π Version & Regression Information
β― Playground Link
Playground Link
π» Code
π Actual behavior
b
is kept in all exported function signatures, even though it is never used. Even if we havetypeof b
in the signature of the original function expression, the type printer will remove that and use the resolved type.π Expected behavior
b
is removed from all signatures since it will never be used. Even if the original type usestypeof
the resulting type will resolve any typeof expressionsAdditional information about the issue
Related to #55654
The text was updated successfully, but these errors were encountered: