Last active
November 27, 2021 16:50
-
-
Save Goldziher/54238e6ac4d286217deec884c80e284c to your computer and use it in GitHub Desktop.
Duck typing in typescript
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 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