Skip to content

Instantly share code, notes, and snippets.

@Goldziher
Last active November 27, 2021 16:50
Show Gist options
  • Save Goldziher/54238e6ac4d286217deec884c80e284c to your computer and use it in GitHub Desktop.
Save Goldziher/54238e6ac4d286217deec884c80e284c to your computer and use it in GitHub Desktop.
Duck typing in typescript
interface Duck {
quack(): string;
}
function isDuck(value: unknown): value is Duck {
return !!(
value &&
typeof value === 'object' &&
typeof Reflect.get(value, 'quack') === 'function'
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment