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.

🌐
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.
Find elsewhere
🌐
GitBook
basarat.gitbook.io › typescript › recap › null-undefined
Null vs. Undefined | TypeScript Deep Dive
Since attributes set to null are encoded, you can transmit the intent to clear an attribute by setting its value to null before encoding and transmitting the object to a remote store. Setting attribute values to undefined can save on storage and transmission costs, as the attribute names will not be encoded. However, this can complicate the semantics of clearing values vs. absent values. TypeScript team doesn't use null : TypeScript coding guidelines and it hasn't caused any problems.
🌐
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.
🌐
GitHub
yellowduck.be › posts › null-vs-undefined-in-typescript-and-javascript-whats-the-difference
🐥 Null vs. Undefined in TypeScript and JavaScript: what's the difference?
Use null when you want to explicitly indicate that a value is absent. Avoid unnecessary undefined assignments. Enable strictNullChecks in TypeScript to catch potential issues.
🌐
Medium
medium.com › @o_o_o › null-vs-undefined-can-i-use-only-one-a3b7db5468f2
null vs. undefined: Can I use only one? | by OOO | Medium
March 29, 2022 - If you create useRef with undefined, ... element’s ref property is expecting and throws error. Typescript returns RefObject<T> only if useRef is initialized with null....
🌐
Tektutorialshub
tektutorialshub.com › home › typescript › null vs undefined in typescript
Null Vs Undefined in TypeScript - Tektutorialshub
March 15, 2023 - But TypeScript never assigns null to any variable. We have to assign Null to variable to make it null. The following example declares the variable foo. We have not given it any initial value. By default, it gets the value undefined.
🌐
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.
🌐
GitHub
github.com › microsoft › TypeScript › issues › 9653
Guidelines for choosing between `null` and `undefined` with `strictNullChecks` · Issue #9653 · microsoft/TypeScript
June 10, 2016 - Whenever you want to make a type optional you have to choose what value to use for the missing values: undefined: Has the benefit that is already there when you don't write anything. lib.d.ts contains 12 references of | undefined. null: ...
Published   Jul 12, 2016
🌐
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/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.