Skip to content
Nate
Stephens

Optional Properties

Optional properties can be left out. If so, their type (if checked) is undefined.

function printCar(car: {
  make: string;
  model: string;
  year: number;
  chargeVoltage?: number; // 'number | undefined'
}) {
  // ...
}

// Works
printCar({
  make: 'Honda',
  model: 'Accord',
  year: 2017,
});

// Also works
printCar({
  make: 'Tesla',
  model: 'Model 3',
  year: 2020,
  chargeVoltage: 220,
});

However, just b/c the type of chargeVoltage is number | undefined doesn't mean you can directly type it that way and get the same result. The property is only optional if you use the ?

function printCar(car: {
  make: string;
  model: string;
  year: number;
  chargeVoltage: number | undefined; // <- no longer optional
}) {
  // ...
}

// ERRORS !!
printCar({
  make: 'Honda',
  model: 'Accord',
  year: 2017,
});
// Error: Property 'chargeVoltage' is missing in type ...

// You would have to include it as 'undefined'
printCar({
  make: 'Tesla',
  model: 'Model 3',
  year: 2020,
  chargeVoltage: undefined, // <- always needs to be included
});

From the TypeScript Fundamentals, v3 course on FEM taught by Mike North.


Last Updated: