You can now use the non-null assertion operator that is here exactly for your use case.

It tells TypeScript that even though something looks like it could be null, it can trust you that it's not:

let name1:string = person.name!; 
//                            ^ note the exclamation mark here  
Answer from yannick1976 on Stack Overflow
🌐
TypeScript
typescriptlang.org › docs › handbook › release-notes › typescript-2-0.html
TypeScript: Documentation - TypeScript 2.0
TypeScript has two special types, Null and Undefined, that have the values null and undefined respectively. Previously it was not possible to explicitly name these types, but null and undefined may now be used as type names regardless of type checking mode.
🌐
W3Schools
w3schools.com › typescript › typescript_null.php
TypeScript Null & Undefined
Even with strictNullChecks enabled, by default TypeScript assumes array access will never return undefined (unless undefined is part of the array type).
🌐
TypeScript
typescriptlang.org › docs › handbook › advanced-types.html
TypeScript: Documentation - Advanced Types
undefined;Type 'undefined' is not assignable to type 'string | null'.2322Type 'undefined' is not assignable to type 'string | null'.Try · Note that TypeScript treats null and undefined differently in order to match JavaScript semantics.
🌐
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?

🌐
Tektutorialshub
tektutorialshub.com › home › typescript › undefined in typescript
Undefined in TypeScript - Tektutorialshub
March 15, 2023 - Undefined is a special value and also a data type in TypeScript. TypeScript also has a global variable with the name undefined which has the value Undefined. Undefined is a primitive value and is treated as falsy for boolean operations.
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › typescript-null-and-undefined-type
TypeScript null and undefined Type - GeeksforGeeks
April 28, 2025 - By default, TypeScript is more permissive with null and undefined. However, when the strictNullChecks option is enabled, variables and properties must be explicitly typed as either nullable or non-nullable.
🌐
GitConnected
levelup.gitconnected.com › when-to-use-the-question-mark-or-undefined-type-in-typescript-233e74ab436
When to use the question mark or undefined type in TypeScript | by Ian Spryn | Level Up Coding
January 17, 2023 - For the other attribute that is marked as string | undefined, you must explicitly pass undefined as your value if you do not pass a string. Let’s declare a new emptyUserWithQuestionMark. const user: UserWithQuestionMark = {}; // no errors · It’s okay for this to be an empty object. However, if we try it with UserNoQuestionMark: /* Property 'name' is missing in type '{}' but required in type 'UserNoQuestionMark'.
Find elsewhere
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
If you don’t specify a type, it will be assumed to be any. Object types can also specify that some or all of their properties are optional. To do this, add a ? after the property name: ... In JavaScript, if you access a property that doesn’t exist, you’ll get the value undefined rather than a runtime error. Because of this, when you read from an optional property, you’ll have to check for undefined before using it. ... TypeScript’s type system allows you to build new types out of existing ones using a large variety of operators.
🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-type-undefined-is-not-assignable-to-type
Type 'undefined' is not assignable to type in TypeScript | bobbyhadz
This means that the property can store a number or an undefined value. The salary variable is typed as a number, so it only expects to get assigned a value that is a number. TypeScript is basically telling us that the emp.salary property might have a value of undefined which is not compatible with the type of the salary variable which only expects a number.
🌐
Ataccama
jobs.ataccama.com › blog › how-to-convert-object-props-with-undefined-type-to-optional-properties-in-typescript
How to Convert Object Props With Undefined Type to Optional Properties in TypeScript | Careers at Ataccama
This type will map over the object props and check if their type contains undefined (by comparing the original prop type with the prop type without undefined type). If yes, it will return never type, effectively removing the key. In other cases, it will return the key of a given property. And that’s it! Once we have mandatory keys we can easily extract mandatory properties from the object type using Pick utility type and combine it with Partial type to have our optional types truly optional. And TypeScript is happy as well.
🌐
TkDodo
tkdodo.eu › blog › optional-vs-undefined
optional vs. undefined | TkDodo's blog
Since Version 4.4, TypeScript has a compiler option called exactOptionalPropertyTypes that can help us distinguish a bit better between the two cases for objects. With the flag turned on, we cannot pass undefined explicitly to a property that is marked as optional.
🌐
www.java4coding.com
java4coding.com › contents › typescript › typescript-null-and-undefined-type
TypeScript null and undefined Type - java4coding
Undefined and null are types and as well as values. When --strictNullChecks argument is used while TypeScript compiling, to assign undefined value, variable should be declared with undefined data type. To assign null value, variable should be declared with null data type.
🌐
Tektutorialshub
tektutorialshub.com › home › typescript › null vs undefined in typescript
Null Vs Undefined in TypeScript - Tektutorialshub
March 15, 2023 - TypeScript has two special values for Null and Undefined. Both represent no value or absence of any value. The difference between Null & Undefined is subtle and confusing. Prior to TypeScript 2.0, we could assign them all other types like numbers, ...
🌐
Atomic Spin
spin.atomicobject.com › optional-undefined-typescript
How to Deal with “Optional” and “Undefined” in TypeScript
November 25, 2024 - When you have a JavaScript object and you ask for a property that doesn’t exist, JavaScript will return undefined rather than throwing an error. In strict mode, this means a couple of things. First, if you don’t tell TypeScript that a property is optional, it will expect it to be set. type Foo = { bar: number; } const a: Foo = {}; // This is an error: // Property 'bar' is missing in type '{}' but required in type 'Foo'.
🌐
TypeScript
typescriptlang.org › docs › handbook › basic-types.html
TypeScript: Handbook - Basic Types
In TypeScript, both undefined and null actually have their types named undefined and null respectively.
🌐
Palantir
palantir.com › docs › foundry › functions › undefined-values
Functions • TypeScript v1 • Handle undefined values • Palantir
1 2 3 4 5 6 7 @Function() public getFullName(employee: Employee): string { if (!(employee.firstName && employee.lastName)) { throw new UserFacingError("Cannot derive full name because either first or last name is undefined."); } return employee.firstName + " " + employee.lastName; } By checking that both the firstName and lastName fields are defined, the TypeScript compiler knows that the final line with the return statement can compile correctly. The benefit of this approach is that type checking is more explicit, and in the case where undefined values are present, you can throw a more explicit error about what went wrong.
🌐
Zipy
zipy.ai › blog › debug-typescript-null-or-undefined-value-errors
Solving Typescript Null & Undefined Errors: Expert Tips
February 28, 2024 - Null or Undefined Value Errors in TypeScript occur when the TypeScript compiler expects a value but finds either null or undefined instead. This discrepancy usually happens because TypeScript is designed to enforce strict typing, ensuring that ...