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

.d.ts emitted declarations should not include local variable names from parameter destructuring #57841

Closed
6 tasks done
octogonz opened this issue Mar 19, 2024 · 2 comments
Closed
6 tasks done

Comments

@octogonz
Copy link

octogonz commented Mar 19, 2024

πŸ” Search Terms

parameter destructuring rename .d.ts declaration

βœ… Viability Checklist

⭐ Suggestion

When using parameter destructuring, a function's parameter list includes the names of local variables. The compiler should not include these local variable names in the generated .d.ts file, because they are an internal implementation detail. Although omitting the local variables would have no operational effect on the declaration, doing so would improve readability, particularly when these signatures end up on an API documentation website.

πŸ“ƒ Motivating Example

Consider this API source code:

book.ts

export interface Book {
  title: string;
}

export function printTitle({ title: logMessage }: Book): void {
    console.log(logMessage);
}

The compiler emits this .d.ts file which includes both title (the destructuring source field) and logMessage (the target local variable):

book.d.ts

export interface Book {
    title: string;
}
export declare function printTitle({ title: logMessage }: Book): void;

πŸ’» Use Cases

  1. What do you want to use this for?

    logMessage is an implementation detail. It increases the size of the .d.ts file without having any observable effect to consumers of the printTitle() API.

  2. What shortcomings exist with current approaches?

    Imagine the above signature is displayed on an API documentation website or VS Code IntelliSense preview. The audience reading this signature may be confused by variables such as logMessage. What is that? Where are the docs for logMessage.

    API Extractor also triggers an API review workflow whenever the type signature changes. Omitting implementation details from the .d.ts signature might avoid some false alarms for inconsequential changes.

  3. What workarounds are you using in the meantime?

    A .d.ts rollup tool such as API Extractor could automatically trim these extraneous tokens. However safely trimming them is a nontrivial operation as the AST changes in each compiler release. It would be much better for the compiler to implement this feature.

@fatcerberus
Copy link

fatcerberus commented Mar 19, 2024

Duplicate of #56992. See #57020 and linked issues. This change was made but had to be reverted due to unforeseen consequences in common edge cases.

@octogonz
Copy link
Author

Duplicate of #56992. See #57020 and linked issues. This change was made but had to be reverted due to unforeseen consequences in common edge cases.

Wow, that is indeed unforeseen. πŸ˜„ Especially that the edge cases could be solved but the computational cost is not justified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants