Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix error TS1015: Parameter cannot have question mark and initializer?

Tags:

typescript

If you look in the TypeScript Language Specification document you can find a wealth of detail about the language syntax.

Section 3.9.2 describes call signatures i.e. the syntax used to call functions and constructors etc.

Section 3.9.2.2 is specifically about the parameters associated with a call.

It defines optional parameters as:

AccessibilityModifieropt BindingIdentifierOrPattern ? TypeAnnotationopt

AccessibilityModifieropt BindingIdentifierOrPattern TypeAnnotationopt Initializer

We can see see that either using '?' OR providing a default value Initializer will mark the parameter as being optional.

So to fix the compiler error you can simply remove the '?' and leave the default value and it will remain as an optional parameter as you intend.