Skip to content

Instantly share code, notes, and snippets.

@ThaddeusJiang
Created June 26, 2021 04:08
Show Gist options
  • Save ThaddeusJiang/dc84edc850772d405ae8a7520fd2cd5a to your computer and use it in GitHub Desktop.
Save ThaddeusJiang/dc84edc850772d405ae8a7520fd2cd5a to your computer and use it in GitHub Desktop.
TypeScript assert Type
type A {
 a: string,
}

type B {
 b: number
}

const data: A | B = { a: 'Hello' }

if((data as A).a !== undefind) {
  // data is A
}
@ThaddeusJiang
Copy link
Author

调查了很多方法,这个方法最简单。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment