How to define optional constructor arguments with defaults in Typescript

An argument which has a default value is optional by definition, as stated in the docs:

Default-initialized parameters that come after all required parameters
are treated as optional, and just like optional parameters, can be
omitted when calling their respective function

It’s the same for constructors as it is for other functions, so in your case:

export class Test {
    constructor(private foo: string = "foo", private bar: string = "bar") {}
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)