Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to pass for TypeScript optional parameter when there is no exact value to specify, 'undefined' or 'null'? [duplicate]

I have below function in TypeScript (which i can't change the order of the parameters),

user(firstName: string, lastName?: string, address?: string);

When in the case where I need to only pass firstName and address what is the better/suitable/recommended value to pass as the lastname ?

case 1: user("Jack", undefined, "NY");

or

case 2: user("Jack", null, "NY");

What are the pros and cons of each approach ?

like image 763
prime Avatar asked Oct 15 '25 04:10

prime


1 Answers

You should use undefined.

This:

lastName?: string

tells us that lastName parameter has type of string | undefined. So using null would be illegal (in strict mode, which is kinda good). Use undefined.

like image 131
Nurbol Alpysbayev Avatar answered Oct 18 '25 04:10

Nurbol Alpysbayev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!