JS Built-ins
TypeScriptintermediate

Interface β€” Structural Typing Guard

πŸ“– Usage Example
interface Serializable { serialize(): string }
function isSerializable(x: unknown): x is Serializable {
  return typeof x === 'object' && x !== null &&
    typeof (x as any).serialize === 'function'
}

TypeScript's structural typing means any object with the right methods satisfies an interface at runtime.

Structural Typing at Runtime

TypeScript uses structural typing β€” but at runtime, you must check the shape manually.

Challenge: Implement isConformingShape(value) returning true if value has a name property of type string.

isConformingShape({ name: 'Alice' })  // β†’ true
isConformingShape({ name: 42 })       // β†’ false
πŸ’‘ Hints (1)
  • β€’ Check object, not null, then property existence, then property type.