Skip to content

Instantly share code, notes, and snippets.

@terite
Last active March 12, 2020 09:30
Show Gist options
  • Save terite/47e5d725bae07723fb11ea5b4a470a65 to your computer and use it in GitHub Desktop.
Save terite/47e5d725bae07723fb11ea5b4a470a65 to your computer and use it in GitHub Desktop.
[ignore]
[include]
[libs]
[lints]
[options]
[strict]
nonstrict-import
unclear-type
unsafe-getters-setters
untyped-import
untyped-type-import
$ npx flow check --show-all-branches
Error --------------------------------------------------------------------------------------------------- index.js:11:29
Cannot call await with `fs.promises.stat(...)` bound to `p` because:
- Either cannot assign `await fs.promises.stat(...)` to `b` because `Stats` [1] is incompatible with number [2].
- Or cannot assign `await fs.promises.stat(...)` to `b` because `Promise` [3] is incompatible with number [2].
index.js:11:29
11| const b: number = await fs.promises.stat('/')
^^^^^^^^^^^^^^^^^^^^^
References:
/private/tmp/flow/flowlib_3a8cf718/node.js:1302:40
1302| stat(path: FSPromisePath): Promise<Stats>,
^^^^^ [1]
index.js:11:14
11| const b: number = await fs.promises.stat('/')
^^^^^^ [2]
/private/tmp/flow/flowlib_3a8cf718/node.js:1302:32
1302| stat(path: FSPromisePath): Promise<Stats>,
^^^^^^^^^^^^^^ [3]
Error ---------------------------------------------------------------------------------------------------- index.js:23:5
Cannot get `b.then` because property `then` is missing in `Number` [1].
index.js:23:5
23| b.then // error is here, in the correct spot
^^^^^^
References:
index.js:4:13
4| const F_OK: number = fs.F_OK
^^^^^^ [1]
Error ---------------------------------------------------------------------------------------------------- index.js:27:5
Cannot get `c.then` because property `then` is missing in `Number` [1].
index.js:27:5
27| c.then // error is here, in the correct spot
^^^^^^
References:
/private/tmp/flow/flowlib_3a8cf718/core.js:133:13
133| length: number;
^^^^^^ [1]
Error --------------------------------------------------------------------------------------------------- index.js:30:21
Cannot call await with `fs.F_OK` bound to `p` because:
- Either number [1] is incompatible with `Promise` [2].
- Or cannot get `d.then` because property `then` is missing in `Number` [1].
index.js:30:21
30| const d = await fs.F_OK // error is here, the wrong spot
^^^^^^^
References:
/private/tmp/flow/flowlib_3a8cf718/node.js:1172:21
1172| declare var F_OK: number;
^^^^^^ [1]
/private/tmp/flow/flowlib_3a8cf718/core.js:696:31
696| declare function $await<T>(p: Promise<T> | T): T;
^^^^^^^^^^ [2]
Found 4 errors
// @flow strict
import fs, { type Stats } from 'fs'
const F_OK: number = fs.F_OK
async function testAwaitPromise() {
// good: flow allows specifying the correct type
const a: Stats = await fs.promises.stat('/')
// good: flow forbids specifying an incorrect type
const b: number = await fs.promises.stat('/')
// BUG: missing error here, await will not return a promise
const c: Promise<Stats> = await fs.promises.stat('/')
}
async function testAwaitNonPromise() {
// good: flow knows awaiting non-promises is OK.
const a: number = await F_OK
// good: flow knows awaiting non-promises does not make them promises
const b = await F_OK
b.then // error is here, in the correct spot
// good: flow does not get confused if the awaited value is external, but not imported
const c = await Uint8Array.length
c.then // error is here, in the correct spot
// BUG: flow gets confused if the awaited value is imported
const d = await fs.F_OK // error is here, the wrong spot
d.then
}
{
"private": true,
"dependencies": {
"flow-bin": "0.120.1"
}
}
// @flow strict
import fs from 'fs'
function assertNotNull<T>(x: T | null): T {
if (x === null) throw new Error('Argument null')
return x
}
function testLocalValue() {
const a = assertNotNull(5)
// good, flow knows number has no length
a.length
}
function testImportedValue() {
// BUG: flow gets confused if the provided value is imported
const a = assertNotNull(fs.F_OK) // error is here, the wrong spot
a.length
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment