Skip to content

Instantly share code, notes, and snippets.

@joanna-liana
Created February 27, 2024 16:02
Show Gist options
  • Save joanna-liana/fc2ad7d4c4964893a66c3303e819ebda to your computer and use it in GitHub Desktop.
Save joanna-liana/fc2ad7d4c4964893a66c3303e819ebda to your computer and use it in GitHub Desktop.
TS method shorthand syntax - bivariance pitfall
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