user3547774
user3547774

Reputation: 1699

TypeScript syntax explanation, optional parameters

I recently came across the following function declaration

subscribe(observerOrNext?: PartialObserver<T> | ((value: T) => void), error?: (error: any) => void, complete?: () => void): Subscription;

I understand that '?' means optional , but what does the rest mean, especially the : PartialObserver<T> | ((value: T) => void) part?

Upvotes: 1

Views: 57

Answers (1)

CodeNotFound
CodeNotFound

Reputation: 23220

I understand that '?' means optional , but what does the rest mean, especially the : PartialObserver<T> | ((value: T) => void) part?

The char '|' is known as Union Type and is used here to tell that the observerOrNextparameter can be one of the foolowing type:

  • generic type PartialObserver<T>
  • or a callback function which signature must return nothing e.g. void and accept a parameter value of type T.

The TypeScript documentation for Advanced Types explains in a better way when to use Union Type.

Upvotes: 1

Related Questions