JS Built-ins
TypeScriptbeginner

Interface β€” Optional Properties

πŸ“– Usage Example
interface Point { x: number; y: number; z?: number }
createPoint(1, 2)      // β†’ { x: 1, y: 2 }
createPoint(1, 2, 3)   // β†’ { x: 1, y: 2, z: 3 }

A ? suffix marks an interface property as optional β€” it may be omitted from the object.

Interfaces: Optional Properties

A ? suffix marks a property as optional. The object can exist with or without it.

Challenge: Implement createPoint(x, y, z?) returning a Point with optional z.

createPoint(1, 2)     // β†’ { x: 1, y: 2 }
createPoint(1, 2, 3)  // β†’ { x: 1, y: 2, z: 3 }
πŸ’‘ Hints (1)
  • β€’ Check z !== undefined before including it, to avoid { x, y, z: undefined }.