Search code examples
typescriptdefault-value

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?


Solution

  • 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