Skip to content

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

Closed
@octogonz

Description

@octogonz

πŸ” 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions