Created
December 24, 2020 15:42
-
-
Save tmatz/73a6e2fe6e8df7b63e3f8352daface26 to your computer and use it in GitHub Desktop.
typescript type guard
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 A { | |
a: string | |
} | |
interface B extends A { | |
b: string | |
} | |
const isB = <T>(v: T): | |
v is (T & B) => 'b' in v | |
const a: A = { a: 'a' } | |
const b: B = { a: 'a', b: 'b' } | |
console.log(isB(a)) | |
console.log(isB(b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment