0

Why this code do not fail when execute a = b ? Play link

type A = {
    x?: number
    y?: number
}

type B = {
    x?: number
    z: number
}

let a: A = {}
let b: B = {z: 1}

a = b // not fail
a = {z: 1} // fail
rjurado01
  • 5,337
  • 3
  • 36
  • 45
  • https://www.typescriptlang.org/docs/handbook/interfaces.html#excess-property-checks – VLAZ Aug 23 '22 at 13:07

1 Answers1

1

Typescript uses structural typing (Wikipedia), an object of type B is a valid A object.

In your second example, excess property checks will prevent you to assign a literal that has nothing in common with A.

DerStoffel
  • 2,553
  • 2
  • 15
  • 25
Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134