HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptCritical

What is the question mark for in a Typescript parameter name

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
typescriptnamemarktheforparameterwhatquestion

Problem

export class Thread {
  id: string;
  lastMessage: Message;
  name: string;
  avatarSrc: string;

  constructor(id?: string,
              name?: string,
              avatarSrc?: string) {
    this.id = id || uuid();
    this.name = name;
    this.avatarSrc = avatarSrc;
  }
}


In id? what's the ? for?

Solution

It is to mark the parameter as optional.

  • TypeScript handbook https://www.typescriptlang.org/docs/handbook/2/functions.html#optional-parameters



  • TypeScript Deep Dive https://basarat.gitbook.io/typescript/type-system/functions#optional-parameters

Context

Stack Overflow Q#37632760, score: 353

Revisions (0)

No revisions yet.