How to fix error TS1015: parameter cannot have a question mark and initializer?

I just upgraded to TypeScript beta 0.9 and now I get the error message:

TS1015: The parameter cannot have a question mark and initializer

It was really before, how can I fix it?

Here is an example of the code generating this error:

functionName(parameterName?: typeName = defaultValue): typeName 
+7
source share
1 answer

Short answer: use value ? OR default - both options will be optional.

Longer answer: If you look in the TypeScript Language Specification document, you can find many details about the language syntax.

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

Section 3.7.2.2 specifies the parameters associated with the call.

It defines additional parameters as:

PublicOrPrivateopt ID? Typeannotationopt

Type ID PublicOrPrivateopt TypeAnnotationopt Initialiser

We see either the use of '?' OR, providing a default value, marks the parameter as optional.

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

+25
source

All Articles