Created
February 27, 2024 16:02
-
-
Save joanna-liana/fc2ad7d4c4964893a66c3303e819ebda to your computer and use it in GitHub Desktop.
TS method shorthand syntax - bivariance pitfall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Client { | |
pay(): void; | |
} | |
interface VIP extends Client { | |
fastPay(): void; | |
} | |
interface Cashier { | |
handleAnyClient(client: Client): void; // wider and narrower Clients allowed | |
handleOnlyGeneric: (client: Client) => void; // only Clients allowed | |
} | |
const vipCashier: Cashier = { | |
handleAnyClient: (vip: VIP) => null, | |
handleOnlyGeneric: (vip: VIP) => null, | |
// ^^^^^^ | |
// Types of parameters 'client' and 'client' are incompatible. | |
// Property 'fastPay' is missing in type 'Client' but required in type 'VIP' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment