3

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 ?

prime
  • 14,464
  • 14
  • 99
  • 131
  • 1
    "Better" regarding what? – Ingo Bürk Oct 17 '18 at 06:47
  • @IngoBürk What will be most suitable or recommended one ? Will the both approaches behave the same ? – prime Oct 17 '18 at 06: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. – Ingo Bürk Oct 17 '18 at 06: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) – prime Oct 17 '18 at 06: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. – Ingo Bürk Oct 17 '18 at 06:59
  • @IngoBürk Yeah agree with that point. Any particular reason why you rule out `null` and let `undefined` be the safest ? – prime Oct 17 '18 at 07:00
  • Because null isn't the same as undefined, so without knowing the implementation we just don't know what the function would do on null. It might just throw an error. Maybe. As I said, it depends on how it's implemented. – Ingo Bürk Oct 17 '18 at 07:03
  • @IngoBürk yeah. thank you for your thoughts. – prime Oct 17 '18 at 07:04
  • Always use undefined. When you're not passing optional parameters `undefined` is passed automaticaly. When you have to pass optional parameters, like in your case, use `undefined` manually. `define testMethod(id, name?)` ---> `call testMethod(1)` ---> `name === undefined` --> `arguments[1] === undefined` – Roberto Zvjerković Oct 17 '18 at 08:05
  • If neither optional parameters are supplied then both will be `undefined`. So if you want to skip the first and supply the second then `undefined` seems like the best option because that's already what the function should be expecting as a possible incoming value. Also, you might be interested in turning on [strict null checks](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html) which will prevent `null`s from being passed to things expecting `undefined`. –  Oct 17 '18 at 12:18

2 Answers2

3

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.

Nurbol Alpysbayev
  • 19,522
  • 3
  • 54
  • 89
  • It's only illegal if you have [strictNullChecks](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html) enabled, which is a good idea IMO. –  Oct 17 '18 at 12:20
  • 1
    @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. – Nurbol Alpysbayev Oct 17 '18 at 12:22
  • Yep, I do the same. –  Oct 17 '18 at 12:23
-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.

Community
  • 1
  • 1
prime
  • 14,464
  • 14
  • 99
  • 131
  • 3
    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" –  Oct 17 '18 at 12:11
  • *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 – Nurbol Alpysbayev Oct 17 '18 at 12:25
  • @NurbolAlpysbayev I think it would not do any harm right ? – prime Oct 17 '18 at 12:30
  • 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. – Nurbol Alpysbayev Oct 17 '18 at 12:33
  • @NurbolAlpysbayev agree on your understanding. I just needed to highlight the difference between those two. – prime Oct 17 '18 at 12:52
  • Still this is extremely frustrating. Why isn't it prescribed that the whole TS community uses only one of them? How should I deal with libraries that use `T | null` to represent optional types (vs. `T | undefined`)? Do I really have to return `variable || undefined` every time `T | undefined` is expected but `variable` is of type `T | null`? Which is _essentially the same thing_? – sleighty Nov 25 '22 at 23:40