Ryan Peschel
Ryan Peschel

Reputation: 11756

Is there any value in including a question mark in a default parameter in TypeScript?

This is a very simple question, so I'll be brief. Is there any difference in these two code snippets?

class Test {
  constructor(value?: number = 5) { }
}

class Test {
  constructor(value: number = 5) { }
}

My assumption is that there is none, because including the default initialized value implicitly makes it also an optional parameter. If that is indeed the case, is it better to include the question mark, or not?

Upvotes: 2

Views: 682

Answers (1)

Alex Wayne
Alex Wayne

Reputation: 187034

Your first snippet is actually a type error.

Parameter cannot have question mark and initializer.(1015)

So I think that should settle it.

See playground

Upvotes: 3

Related Questions