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.
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?
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
Videos
export interface UserTable {
user_id: Generated<number>,
alternate_user_id: Generated<number> | null,
personnel_id: number | undefined,
company_id: number | null,
email: string | undefined,
email_verified: boolean | null
}When creating a type definition what am I supposed to use to indicate that a column might not have a value at creation? Undefined? Null? I figure I will include ' | null' for every column that is nullable? So what is the purpose of undefined in type definitions??
I am using Kysely, and Postgresql
EDIT: The above code isn't real, just filler as an example.
Maybe our GraphQL schema is bad, I don't know. I just work on the frontend.
I'm having problems like this when I turn strict non-null checks on:
Type 'Maybe<number>' is not assignable to type 'number | undefined'. Type 'null' is not assignable to type 'number | undefined'.ts(2322)
Variable that I have is from GraphQL codegen: Maybe<number>
I want to put it into here?: number;
Can I do something graphQL codegen settings? Preferably.
I can't play with the schema itself, not easily.
I can't change the whole app and turn everything into here?: number | null; that is too verbose and feels stupid, there must be a better way.
Hello guys, i need your help, i would like to know which you think is a better approach:
1 --------------------------------------------
const {content} = props;
if(content !== "undefined"){
//code
}
2 --------------------------------------------
const {content = null} = props;
if(!content){
//code
}
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.
What do people think of the ability to have multiple types like this? I found it super annoying when mapping between objects. If the same property names are used but one is string but the other property from the other class is string | null, the IDE complains. Are there any situations where you've found it helpful to be able to declare at type like this?
protected combineAndSave(
audiosAndPauseFiles: Array<string>
, savePath: string
, filePrefix?: string
, fileName?: string
): void {I noticed when calling this, I had to specify undefined on the 3rd arg (4th will be implicitly undefined if not specified). Is that the best way to do it? In general I think null is more explicit but maybe that does apply for this case.
Also if anyone knows a way to more tightly constrain this method so that one of the last two params must be specifed, I appreciate any further advice on that front.
What do you prefer to check for null and undefined?
Feel free to comment on why which one is better.
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
In my Express server, I have the situation that certain request properties are guaranteed by middleware, but the compiler doesn't know.
Hence, req.user could be "possibly undefined" even though I have middleware that checks for it before every request:
Lint doesn't like the ! operator to assert non-nullishness. But it doesn't complain when using as:
My question here: Are there any actual benefits of using the as operator here? Or is it effectively exactly the same?
I want to create a type that loops through an object and replaces all nulls with undefined. For example:
// input:
// {
// a: string | null;
// b: string;
// }
// returns:
// {
// a: string | undefined;
// b: string;
// }The best I Have so far is this
export type ReplaceNullWithUndefined<T extends Object> = {
[key in keyof T]: Extract<T[key], null> extends null
? Exclude<T[key], null> | undefined
: T[key];
};
// which with input:
// {
// a: string | null;
// b: string;
// }
// returns:
// {
// a: string | undefined;
// b: string | undefined;
// }Thank you, any help would be amazing