Google News
logo
TypeScript - Interview Questions
How to specify optional properties in TypeScript?
An object type can have zero or more optional properties by adding a ‘?’ after the property name. 
let pt: { x: number; y: number; z?: number } = {
  x: 10,
  y: 20
};
console.log(pt);
In the example above, because the property ‘z’ is marked as optional, the compiler won’t complain if we don’t provide it during the initialization.
Advertisement