Skip to content

Invalid type declaration for Class Instance Method inside a .js file with a default value of an inner parameter #36507

Closed

Description

TypeScript Version: 3.7.5 and 3.8.0-dev.20200128

Search Terms: "default value" parameter "type definition"

Code

tsc --allowJs --declaration --outDir out x.js

x.js:

export class Z {
  f(x = 1, y) {
    return [x, y];
  }
}

Expected behavior:

x.d.ts should be:

export class Z {
    f(x: number | undefined, y: any): any[];
}

Actual behavior:

x.d.ts contains invalid typescript:

export class Z {
    f(x?: number, y: any): any[];
}

Playground Link: Playground Link

Related Issues: None

Notes: Both top-level functions (in either .js or .ts) and class instance methods in .ts files have correct type defs generated in both 3.7.5 and 3.8.0-dev.20200128

Activity

  1. a-tarasyuk commented on Feb 3, 2020

    @a-tarasyuk
    Contributor

    Should TS emit x: number | undefined (x?) in strict mode? Or here strict option doesn't matter?

    сс Ryan Cavanaugh (@RyanCavanaugh) Wesley Wigham (@weswigham) Daniel Rosenwasser (@DanielRosenwasser)

  2. weswigham commented on Feb 3, 2020

    @weswigham
    Member

    Emitting the undefined even outside strict mode is, IMO fine; but I'm not 100% sure if that's desirable (since the code in question may not actually handle undefined)

  3. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions