Skip to content

Instantly share code, notes, and snippets.

@KSXGitHub
Created October 18, 2018 17:51
Show Gist options
  • Save KSXGitHub/7b252cd08ba748548c45ef5ef486bda0 to your computer and use it in GitHub Desktop.
Save KSXGitHub/7b252cd08ba748548c45ef5ef486bda0 to your computer and use it in GitHub Desktop.
Type Guard experimentation of Set
interface XSet<X0> {
has<X1 extends X0> (x: X1): this is XSetWith<X1, X0>
}
interface XSetWith<X1 extends X0, X0> extends XSet<X0> {
has<X2 extends X0> (x: X2): this is XSetWith<X2, X1>
has<X2 extends X1> (x: X2): true
}
declare const xset: XSet<number>
if (xset.has(2)) {
const f = xset.has(2) // true
const g = xset.has(3) // boolean
if (xset.has(7)) {
const ff = xset.has(2) // true
const hh = xset.has(7) // true
const ii = xset.has(8) // boolean
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment