TypeScript Tutorial

TypeScript Strict Null

null and undefined are not assignable to every type. Check them, or the compiler stops you.

Absent is a type, not a surprise

In JavaScript, null and undefined can show up where you expected a string or a number. With strict (the mode StudyGrid uses), TypeScript does not let you assign them tostring or number unless you say so. That is strictNullChecks: missing values are not every type.

C++ memory mistakes are often a dangling pointer or a leak. TypeScript’s closest beginner analog is using a value that might be missing. The compiler asks you to check first. The program that runs has already dropped the types; the check is for you at compile time.

A union that includes null

Write string | null when a name might be missing. You cannot call toUpperCase until you narrow. An if that compares to null is enough. Inside the else (or after a return), tsc treats the value as string.

Example

function label(name: string | null): string {
  if (name === null) {
    return "guest";
  }
  return name.toUpperCase();
}

console.log(label("Ada"));
console.log(label(null));

Output is ADA then guest. let who: string = null; does not compile under strict. That is the feature. If the value can be missing, put | null (or | undefined) on the type.

Compile this at /typescript/try. Try return name.toUpperCase() as the first line oflabel with no check. tsc reports that name is possibly null.

?? picks a fallback

The nullish coalescing operator ?? uses the right-hand value when the left is null orundefined. It does not treat 0 or "" as missing. That differs from||, which also replaces zeros and empty strings.

Example

const nickname: string | undefined = undefined;
const tries: number | undefined = 0;
console.log(nickname ?? "anon");
console.log(tries ?? 3);
console.log(tries || 3);

First line anon. Second line 0 — zero is present, so ?? keeps it. Third line 3, because || treats 0 as false. Prefer ?? when0 is a real measurement.

?. stops before a missing property

Optional chaining ?. reads a property only if the value on the left is not null orundefined. Otherwise the whole expression is undefined. Pair it with ??when you want a printed fallback.

Example

type Team = { city: string };
type Player = { name: string; team?: Team };

const ada: Player = { name: "Ada", team: { city: "London" } };
const kai: Player = { name: "Kai" };

console.log(ada.team?.city ?? "no team");
console.log(kai.team?.city ?? "no team");

Output is London then no team. kai.team.city without ?. does not compile: team might be undefined. The optional field is team? on the type.

null versus undefined

ValueTypical meaning
undefinedNot set: a missing optional property, or a missing argument.
nullSet, and intentionally empty: a cleared field from an API.
??Fallback only for null or undefined.
?.Read a property only if the object is present.

Pick one missing marker in a given API when you can. Mixing both works if you check with == null(that test catches both) or with ??. This tutorial writes the union out:string | null or string | undefined.

Do not silence the check

name! (the non-null assertion) tells tsc you are sure the value is present. If you are wrong, you get a runtime crash. Prefer an if, ??, or ?.. The type-assertions chapter later covers as for other cases; do not use it as a habit to hide null.

StudyGrid compiles with strict: true, which includes strict null checks. Locally, pass--strict to tsc so you see the same errors.

Check, then use

If a value might be missing, the type should say so. Narrow it before you call a method. Next on this track is a shelf of complete programs at TypeScript Examples, then the ternary operator for choosing one of two values.

FAQ: TypeScript Strict Null

Common questions about this page.

What is the StudyGrid TypeScript tutorial?

The StudyGrid TypeScript tutorial follows the same chapter rhythm as C++: syntax, types, input, loops, functions, classes, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run typescript strict null examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn typescript strict null in this TypeScript TypeScript lesson (TypeScript Strict Null).

Is the TypeScript editor the same as Try Python or Try C++?

No. Try TypeScript type-checks with tsc at /typescript/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. TypeScript lessons never open those editors.

Do I need to install a compiler to learn TypeScript?

No. Open a chapter, click Try it in TypeScript, and compile in the browser. You can also download a .ts file and compile locally with tsc.

Where should I start the TypeScript tutorial?

Start at TypeScript Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open TypeScript Examples, then generics, Map, and arrow functions. Use Next at the bottom of each chapter.

Is the TypeScript tutorial free?

Yes. The TypeScript workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.