parameter?: type | undefined vs parameter?: type

Viewed 28

I'm trying to understand the difference between two ways of declaring an optional parameter for a function:

parameter?: type

and

parameter?: type | undefined

From what I understand, the first is a shorthand for

parameter: type | undefined = undefined

Is the second way any different from this?

Thanks!

1 Answers

An optional parameter resolves its parameter to undefined if nothing has been passed.

Therefore, the second statement is redundant as it is an optional parameter but also allows 'type' and undefined.

No difference in what it does as long as type is strictly typed.

Related