Created
May 23, 2018 00:06
-
-
Save leebyron/5c2c68aaa7e303b0c319fbd5ffe764ea to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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