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.

Answer from Spitzbueb on Stack Overflow
🌐
Reddit
reddit.com › r/typescript › undefined vs null
r/typescript on Reddit: Undefined vs null
February 27, 2023 -

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?

🌐
Reddit
reddit.com › r/typescript › maybe null vs undefined
r/typescript on Reddit: Maybe<number> null vs undefined
December 24, 2022 -

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.

🌐
Reddit
reddit.com › r/typescript › null v undefined in type definitions for sql tables
r/typescript on Reddit: Null v undefined in type definitions for SQL tables
November 7, 2024 -
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.

🌐
Reddit
reddit.com › r/javascript › null vs undefined
r/javascript on Reddit: null vs undefined
June 21, 2017 - ... I try to think of it simply as: undefined means exactly that, we have not bothered to set a value for this thing. null means we explicitly set a value and wanted it to be anomalous.
Find elsewhere
🌐
Reddit
reddit.com › r/typescript › typescript: the many types of nothing - a post about the technical differences between null, undefined, void, never and unknown, and their runtime variants
r/typescript on Reddit: TypeScript: The many types of nothing - a post about the technical differences between null, undefined, void, never and unknown, and their runtime variants
December 31, 2022 - TypeScript is a language for application-scale JavaScript development. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ... However, void is special in one aspect: A function of type () => void can also return other things than undefined, even functions that never return are valid assignments.
🌐
Reddit
reddit.com › r/typescript › optional parameters, specifying undefined vs null
r/typescript on Reddit: Optional parameters, specifying undefined vs null
July 9, 2020 -
   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.

🌐
DEV Community
dev.to › typescripttv › what-is-the-difference-between-null-and-undefined-5h76
What is the difference between null and undefined? - DEV Community
March 12, 2023 - The convention in TypeScript is that undefined values have not been defined yet, whereas null values indicate intentional absence of a value.
🌐
DEV Community
dev.to › sduduzog › null-vs-undefined-what-to-choose-what-to-use-11g
null vs undefined? What to choose? What to use? - DEV Community
August 23, 2023 - #typescript #javascript #learning · If you have experience with writing JavaScript, you might have a preference of using undefined in place of null, and even go to an extent of casting null values to undefined instead. If you were any wiser, you'd have known that null and undefined are not supposed to be interchangeable.
🌐
W3Schools
w3schools.com › typescript › typescript_null.php
TypeScript Null & Undefined
let value: string | undefined | null = null; value = 'hello'; value = undefined; Try it Yourself » · When strictNullChecks is enabled, TypeScript requires values to be set unless undefined is explicitly added to the type.
🌐
Reddit
reddit.com › r/programmerhumor › null vs undefined
r/ProgrammerHumor on Reddit: null vs undefined
February 15, 2021 - It's ok for functions to have a signature that says 'by the time the variable gets to me, null should not be a possibility I have to worry about', and a good practice since the null possibility should have a limited lifespan, but don't advocate default values if you see null unless a default makes logical sense for the data. ... Idgi, why are you destructuring the type unless you know what you want to do with it? And if you don’t actually want to use a default value if the instance is None, then what the heck would you WANT to do with it? You know that Option types have a map function that’ll propagate None if the instance isn’t inhabited by an actual value, right? ... Typescript kicks ass at this.
🌐
TutorialsPoint
tutorialspoint.com › typescript › typescript_null_vs_undefined.htm
TypeScript - null vs. undefined
In TypeScript, 'undefined' denotes that a variable has been declared but has not been assigned any value. On the other hand, 'null' refers to a non-existent object which is basically 'empty' or 'nothing'.
🌐
Reddit
reddit.com › r/typescript › replacing "null" with "undefined"
r/typescript on Reddit: Replacing "null" with "undefined"
January 6, 2023 -

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