Skip to content

Instantly share code, notes, and snippets.

@tmatz
Created December 24, 2020 15:42
Show Gist options
  • Save tmatz/73a6e2fe6e8df7b63e3f8352daface26 to your computer and use it in GitHub Desktop.
Save tmatz/73a6e2fe6e8df7b63e3f8352daface26 to your computer and use it in GitHub Desktop.
typescript type guard
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