4

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 ?

10
  • 1
    "Better" regarding what? Commented Oct 17, 2018 at 6:47
  • @IngoBürk What will be most suitable or recommended one ? Will the both approaches behave the same ? Commented Oct 17, 2018 at 6:48
  • That depends on how the function is implemented. Using undefined does exactly what would happen if you omitted the second (and third) parameters, but null is shorter (which is also a metric). How the function behaves on either undefined or null depends on the implementation. Commented Oct 17, 2018 at 6:49
  • @IngoBürk In the function it does not explicitly handle that scenario. In that case what should I pass. This function I cannot change. (But I like to know what possible changes can do apart from handling those null and undefined) Commented Oct 17, 2018 at 6:55
  • The safest bet is using undefined. If the function doesn't handle that, it probably shouldn't make those parameters optional in the first place since user("Jack") is the same as user("Jack", undefined, undefined), so why make something optional if it can't handle absent parameters. Commented Oct 17, 2018 at 6:59

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It's only illegal if you have strictNullChecks enabled, which is a good idea IMO.
@AndyJ I forgot about that, because in any project the first thing I do is enabling strict: true. Non-strict typescript is weird for me.
Yep, I do the same.
-1

Well I found this The Typescript coding style guide which states that you should always use undefined and not null,

null and undefined

  • Use undefined. Do not use null.

Anyway check here, undefined means a variable has been declared but has not yet been assigned a value. null is an assignment value. It can be assigned to a variable as a representation of no value. So to serve the purpose of optional I think undefined would be better.

6 Comments

It doesn't state that you should always use undefined, it states you should always use undefined when you're contributing to the TS codebase ... "These are Coding Guidelines for Contributors to TypeScript. This is NOT a prescriptive guideline for the TypeScript community." and they continue to drive the message home "AGAIN: This is NOT a prescriptive guideline for the TypeScript community"
Anyway check here, undefined... The link explains the subject in the javascript context. If you want to know what's correct for TS, please see my answer, it is 100% legit
@NurbolAlpysbayev I think it would not do any harm right ?
The link? If yes, then the link is correct in JS context. Moreover it is correct for Typescript too, because it's superset of JS. But the subject is about Typescript, so I thought you wanted to know what is correct way from the type system point of view.
@NurbolAlpysbayev agree on your understanding. I just needed to highlight the difference between those two.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.