This post explains the differences very well. They are the same in TypeScript as in JavaScript.
As for what you should use: You may define that on your own. You may use either, just be aware of the differences and it might make sense to be consistent.
The TypeScript coding style guide for the TypeScript source code (not an official "how to use TypeScript" guide) states that you should always use undefined and not null: Typescript Project Styleguide.
This post explains the differences very well. They are the same in TypeScript as in JavaScript.
As for what you should use: You may define that on your own. You may use either, just be aware of the differences and it might make sense to be consistent.
The TypeScript coding style guide for the TypeScript source code (not an official "how to use TypeScript" guide) states that you should always use undefined and not null: Typescript Project Styleguide.
The value 'undefined' denotes that a variable has been declared, but hasn't been assigned any value. So, the value of the variable is 'undefined'.
On the other hand, 'null' refers to a non-existent object, which basically means 'empty' or 'nothing'.
You can manually assign the value 'undefined' to a variable, but that isn't recommended. So, 'null' is assigned to a variable to specify that the variable doesn't contain any value or is empty. But 'undefined' is used to check whether the variable has been assigned any value after declaration.
Since switching to TypeScript I have been using a lot of optional properties, for example:
type store = {
currentUserId?: string
}
function logout () {
store.currentUserId = undefined
}However my coworkers and I have been discussing whether null is a more appropriate type instead of undefined, like this:
type store = {
currentUserId: string | null
}
function logout () {
store.currentUserId = null
}It seems like the use of undefined in TypeScript differs slightly from in Javascript.
Do you guys/girls use undefined or null more often? And, which of the examples above do you think is better?
Videos
No rationale is necessary for guidelines, the requirement could be chosen at random in order to keep the code base consistent.
As for this guideline, undefined takes more characters to type but doesn't need to be explicitly assigned to nullable variable or property:
class Foo {
bar: number|undefined;
}
function foo(bar: number|undefined) {}
vs.
class Foo {
bar: number|null = null;
}
function foo(bar: number|null = null) {}
Also, it's less convenient to check the type of null value at runtime because typeof val === 'object' for both null and object value, while it's typeof val === 'undefined' for undefined.
There is relevant TSLint rule that addresses this concern, no-null-keyword.
I made some research few months ago and I came out with the fact that undefined must be prioritize using tslint rule 'no-null-keyword'.
I tried to change my codebase and I had some issues. Why? Because I'm using an API that return null for empty fields.
I struggled because of the tslint triple-equals rule.
if (returnedData === undefined) // will be false because returnedData is null
Which let you 2 options:
1) Add some parameters to your triple-equals rules.
"triple-equals": [true, "allow-null-check"] and do If (returnedData == null)
allow-null-check allow "==" for null
2) Use If (returnedData) instead but it checks if null/undefined/empty string or a zero
Hey! What are your thoughts on explicit returning undefined in a function vs returning null, this in the absence of what you asked the function for.
Let's take these functions as an exampletype getUser = (id: string): User | undefined vs type getUser = (id: string): User | NULL
What are your thoughts on this? Good and bad etc