lecture 6 of 120 completed

Optional properties and undefined

A field marked with a question mark may be missing. The compiler makes you handle that before you use the value.

What you'll learn

  • What the question mark on a field means
  • Three ways to handle a value that may be undefined
  • Why the exclamation mark removes the error and keeps the crash

In Shelf, a loan gets a return date only when the book comes back. A test helper reads the year from that date.

const loan = { book: "Dune" };
console.log(loan.returnedOn.slice(0, 4));
TypeError: Cannot read properties of undefined (reading 'slice')

This is JavaScript, and the wording is from the engine in Node and Chrome. The helper works for every returned book. It stops the test on the first loan that is still open.

TypeScript can report this before the code runs, if the type says the truth about the field.

interface Loan {
  book: string;
  returnedOn?: string;
}

function returnYear(loan: Loan): string {
  return loan.returnedOn.slice(0, 4);
}
loans.ts(7,10): error TS18048: 'loan.returnedOn' is possibly 'undefined'.

What the question mark says

returnedOn?: string marks the field as optional. An object may leave it out and still be a Loan.

For the code that reads the field, the value is then text or undefined. TypeScript writes that type as string | undefined. The | is read as "or". Lecture 8 is about types joined this way.

The compiler does not let you call .slice on a value that may be undefined. You have to say what happens when the date is missing. There are three honest ways.

Check first

interface Loan {
  book: string;
  returnedOn?: string;
}

function returnYear(loan: Loan): string {
  if (loan.returnedOn === undefined) {
    return "not returned";
  }
  return loan.returnedOn.slice(0, 4);
}

console.log(returnYear({ book: "Dune" }));
console.log(returnYear({ book: "Emma", returnedOn: "2026-03-01" }));
not returned
2026

The last line of the function has no error now. TypeScript follows the if. After it, undefined is no longer possible, so the type of loan.returnedOn is plain string. This is called narrowing: a check makes the type smaller.

Optional chaining and a fallback

You know ?. and ?? from the JavaScript track. They work the same here, and the compiler understands both.

interface Loan {
  book: string;
  returnedOn?: string;
}

function returnYear(loan: Loan): string {
  return loan.returnedOn?.slice(0, 4) ?? "not returned";
}

console.log(returnYear({ book: "Dune" }));
not returned

?. stops and gives undefined when the date is missing. ?? then supplies the text to use.

Let the return type say it

Sometimes the helper should not invent a value. Then say so in the return type, and let the caller decide.

interface Loan {
  book: string;
  returnedOn?: string;
}

function returnYear(loan: Loan): string | undefined {
  return loan.returnedOn?.slice(0, 4);
}

Which of the three is right depends on what the caller needs. That is a decision about the test, and the compiler cannot make it for you.

The exclamation mark

There is a fourth way, and you will see it in real projects. An exclamation mark after a value tells the compiler "this is never undefined, trust me".

interface Loan {
  book: string;
  returnedOn?: string;
}

function returnYear(loan: Loan): string {
  return loan.returnedOn!.slice(0, 4);
}

console.log(returnYear({ book: "Dune" }));
TypeError: Cannot read properties of undefined (reading 'slice')

The compiler reports nothing, and the code stops exactly as the JavaScript did. Remember lecture 1: types are removed before the code runs. The ! is removed with them. It changed what the compiler believes and nothing else.

A reviewer will stop at every ! in a test helper and ask how you know. Sometimes you do know. Write the check anyway, because it costs one line and fails with a message you chose.

undefined and null

An API can also send null for "no value". That is a different value, and the type must name the one the data really uses: returnedOn: string | null. Check a real response before you write the type.

This whole check is part of the strict setting. A project without it lets every example in this lecture compile, including the first one.

Remember this

A question mark makes a field optional, and reading it gives the value or undefined. Handle the missing case with a check, with ?. and ??, or in the return type. An exclamation mark only silences the compiler. The crash is still there when the code runs.

Reference: Optional Properties in the TypeScript Handbook.

now prove it

Reading is the setup. Solve this exercise in the editor. We check your code with the TypeScript compiler. Then we run it on each case and compare what it prints with the expected output.