Skip to content

Ambient type for constructor parameter property with default does not include 'undefined' when the parameter is followed by one without a default #48547

@yseymour

Description

@yseymour

Bug Report

🔎 Search Terms

constructor parameter property default undefined

🕗 Version & Regression Information

This is the behaviour in every version in the Playground (3.3 through to 4.6.2)

⏯ Playground Link

Playground link with relevant code

💻 Code

class C { constructor(public a = 123, b: string) {} }

🙁 Actual behavior

.d.ts emit:

declare class C {
    a: number;
    constructor(a: number, b: string);
}

🙂 Expected behavior

Parameter a should be of type number | undefined. Otherwise, it's impossible to initialise the field to its default.

Note that if parameter b is removed, given a default, or made optional, a is then correctly emitted as optional (a?: number). Clearly TypeScript won't emit an optional followed by a required parameter, but it also doesn't add the undefined type.

If a is not a property (remove public) the emitted signature correctly includes undefined.

Secondary bug: undefined is also missing from the type hint for a. However, the compiler correctly accepts undefined when the class is declared inline:

Screen Shot 2022-04-04 at 1 06 18 pm

That differs from the behaviour when only the .d.ts definition is available, where the compiler then rejects undefined in the same position.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Declaration EmitThe issue relates to the emission of d.ts filesHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions