codemix / babel-plugin-typecheck

Static and runtime type checking for JavaScript in the form of a Babel plugin.
MIT License
886 stars 44 forks source link

Support for Enums #127

Closed okonet closed 7 years ago

okonet commented 8 years ago

I'm looking at this test: https://github.com/facebook/flow/blob/f1273c67638b5403973a496e30b4a3fe8dfe19bc/tests/enumerror/enumerror.js

var MyStates = {
    PAUSED: 'PAUSED',
    ACTIVE: 'ACTIVE',
    DELETED: 'DELETED',
};
function isActive2(ad: {state: $Keys<typeof MyStates>}): boolean {
    return ad.state === MyStates.ACTIVE;
};

but trying it my codebase results in Uncaught ReferenceError: $Keys is not defined.

phpnode commented 8 years ago

yeah we don't support those kinds of (un(der)documented) special flow features. This one should be relatively easy to add but I'm not sure if it's a good idea, given that it might not hang around in Flow.

Ugly work around:

type PossibleStates = 'PAUSED' | 'ACTIVE' | 'DELETED';
const MyStates: {[state: PossibleStates]: PossibleStates} = {
    PAUSED: 'PAUSED',
    ACTIVE: 'ACTIVE',
    DELETED: 'DELETED',
};
function isActive2(ad: {state: PossibleStates}): boolean {
    return ad.state === MyStates.ACTIVE;
};
phpnode commented 7 years ago

Hi sorry for the delay, this project is now deprecated in favour of flow-runtime which aims for full flow compatibility. flow-runtime has support for special flow forms such as $Keys and $Shape