Back to questions
JuniorTheoryCommon
On which line does tsc report an error?
interface Point { x: number; y: number }
const a = { x: 1, y: 2, z: 3 };
const p1: Point = a;
const p2: Point = { x: 1, y: 2, z: 3 };
console.log(p1.x, p2.x);4
- Expecting the extra
zto fail through the variable too: only a fresh literal is checked for unknown properties, andaon line 3 is compared by shape. - Expecting extra properties never to matter: a literal written in the assignment itself gets error TS2353 for
z.
- How would you change line 2 so that tsc reports the extra
zthere too? - What does tsc report if the literal from line 4 is passed straight to a function that takes a
Point?