0

How can get a error message from the function in the example below? I have made comments in the relevant place in the code below:

interface Pos {
  x: number,
  y: number,
}

function doSome(pos: Pos) {
  return pos.x + pos.y
}

let pos = {
  x: 1,
  y: 2,
  z: 3,
}

// I want this function to give me an error
// message as 'pos' does contain 'z' which 
// the interface 'Pos' does not.
// The variable 'pos' is not exactly the same
// type as the interface 'Pos', which i want
// to make sure it is in this case.
let p = doSome(pos)
Ólavur Nón
  • 161
  • 1
  • 13

1 Answers1

0

TypeScript only checks for the required properties defined in the interface, that means that, if you have x and y it will be enough to run the function, You may have extra literals, and you will not get the error.

If you need an error, then maybe you can add extra literal in a function that you don't define that literal in the interface, in that case, you will get an error

ilia
  • 98
  • 1
  • 8