Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[flow-enums-runtime] Add definition (#4567)
- Loading branch information
1 parent
388e9ed
commit ca06757
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
definitions/npm/flow-enums-runtime_v0.x.x/flow_v0.83.x-/flow-enums-runtime_v0.x.x.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
declare module 'flow-enums-runtime' { | ||
declare type Members = { +[key: string]: string }; | ||
|
||
declare type EnumPrototype<T> = {| | ||
isValid: (x: $Keys<T>) => boolean, | ||
cast: <V>(x: V) => V | void, | ||
members: () => Array<$Keys<T>>, | ||
getName: (value: string) => string, | ||
|}; | ||
|
||
declare type Enum = {| | ||
<T: Members>(members?: T): {| ...EnumPrototype<T>, ...T |}, | ||
Mirrored: <T: Members>(members: Array<$Keys<T>>) => {| ...Members, ...EnumPrototype<T> |}, | ||
|}; | ||
|
||
declare module.exports: Enum; | ||
} |
39 changes: 39 additions & 0 deletions
39
definitions/npm/flow-enums-runtime_v0.x.x/flow_v0.83.x-/test_flow-enums-runtime_v0.x.x.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// @flow | ||
import { describe, test } from 'flow-typed-test'; | ||
import flowEnumRuntime from 'flow-enums-runtime'; | ||
|
||
describe('flow-enums-runtime', () => { | ||
test('default function', () => { | ||
const obj = { a: 'a' } | ||
|
||
const res = flowEnumRuntime(obj); | ||
|
||
res.a; | ||
res.isValid('a'); | ||
const valid = res.cast('a'); | ||
// $FlowExpectedError[incompatible-use] | ||
valid.toLowerCase(); | ||
if (valid) { | ||
valid.toLowerCase(); | ||
} | ||
(res.members(): Array<$Keys<typeof obj>>); | ||
|
||
// $FlowExpectedError[prop-missing] | ||
res.b; | ||
}); | ||
|
||
test('Mirrored', () => { | ||
const { Mirrored } = flowEnumRuntime; | ||
|
||
const obj = { a: 'b' }; | ||
|
||
Mirrored<typeof obj>(([]: Array<$Keys<typeof obj>>)); | ||
Mirrored<{| a: 'a', b: 'b' |}>(['a', 'b']); | ||
// $FlowExpectedError[prop-missing] | ||
Mirrored<{| a: 'a', b: 'b' |}>(['a', 'b', 'c']); | ||
// $FlowExpectedError[incompatible-call] | ||
Mirrored(); | ||
// $FlowExpectedError[incompatible-call] | ||
Mirrored<{| a: 'a', b: 'b' |}>([1]); | ||
}); | ||
}); |