basarat/typescript-book

Type compatibility example incorrect

lauripiisang opened this issue · 1 comments

The example in https://basarat.gitbook.io/typescript/type-system/type-compatibility#types-of-arguments

This can be confusing for people coming from other languages who would expect the following to error but will not in TypeScript:

/** Type Hierarchy */
interface Point2D { x: number; y: number; }
interface Point3D { x: number; y: number; z: number; }

/** Two sample functions */
let iTakePoint2D = (point: Point2D) => { /* do something */ }
let iTakePoint3D = (point: Point3D) => { /* do something */ }

iTakePoint3D = iTakePoint2D; // Okay : Reasonable
iTakePoint2D = iTakePoint3D; // Okay : WHAT

will actually error when tried out in https://www.typescriptlang.org/play so I don't understand what that example is about. Tried with 3.3.3, 3.9.2, 4.0.2 . Possibly it worked in a previous ts version.

It's fixed if you have the strictFunctionTypes flag enabled (or strict) in tsconfig.json - may be worth making a PR to note this behavior has been patched behind a flag.