Skip to content

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

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

Closed
ghost opened this issue Jan 29, 2020 · 2 comments · Fixed by #37173
Assignees
Labels
Bug A bug in TypeScript

Comments

@ghost
Copy link

ghost commented Jan 29, 2020

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

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jan 31, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.9.0 milestone Jan 31, 2020
@a-tarasyuk
Copy link
Contributor

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

сс @RyanCavanaugh @weswigham @DanielRosenwasser

@weswigham
Copy link
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)

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

Successfully merging a pull request may close this issue.

3 participants