Skip to content

Instantly share code, notes, and snippets.

@leebyron
Created May 23, 2018 00:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leebyron/5c2c68aaa7e303b0c319fbd5ffe764ea to your computer and use it in GitHub Desktop.
Save leebyron/5c2c68aaa7e303b0c319fbd5ffe764ea to your computer and use it in GitHub Desktop.
type HasFoo = { kind: "Foo" }
type HasBar = { kind: "Bar", baz: string }
class Other {
baz = "cool"
}
function hasBar(obj: HasFoo | HasBar | Other): boolean %checks {
return obj instanceof Other
}
declare var z: HasFoo | HasBar | Other
if (hasBar(z)) {
z.baz
}
type MaybePromise<T> = Promise<T> | T
declare function isPromise(obj: mixed): boolean %checks(
obj instanceof Promise
)
function isPromise(obj) {
return obj && typeof obj.then === 'function'
}
declare var x: mixed;
if (isPromise(x)) {
x.then(x => x)
}
const y = { then: x => x }
if (isPromise(y)) {
y.then(x => x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment