-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
typeof
string literal
#2639
Comments
Seems all of the literal types( string, number ...) will be treated as a primitive type at indirectly referring. |
@iamchenxin you are right, they are also not captured by polymorphic functions. |
Here's an example of this in action: Edit: the link I previously had now redirects to an unrelated site. |
In TypeScript behaviour depends on type of binding: const x = 'X';
const y: typeof x = 'Y'; // error let x = 'X';
const y: typeof x = 'Y'; // no error |
To put in another way: const foo = 'foo'
const bar: typeof foo = 'bar' Since |
Yes! To me it also seems like it would be much nicer if |
But what about typeof 'string'? Or is flow's checking using typeof different then javascripts typeof? |
@k so flow's So yes, they are completely different :-) As for |
I dig Typescript's implementation making use of |
+1 When defining reusable consts as module exports, code that needs to be refined still has to compare some variable explicit with string literal. It should be refined using === |
When Flow sees a definition like `const FOO = 'FOO'`, it knows for some purposes that `FOO` is specifically `'FOO'`... but for others, it apparently broadens it to `string`. This is facebook/flow#2639. One consequence is that when we say switch (action.type) { case NOT_FOO_LOL: // ... use `action` ... break; // ... } with a `case` statement for a value of `type` that we've claimed is *impossible*... Flow quietly just decides that `action` has type `empty` and there's nothing to worry about. Then if our code goes and passes that `action` value to *any function at all*, Flow considers it valid. After all, there are no values of type `empty` (that's what `empty` is all about)... so it's true that every value of type `empty` is a valid value of whatever type it is that the function expected. So, that's pretty unfortunate. Fortunately, there is a workaround that causes Flow to behave better here: duplicate the value into the type, like `const FOO: 'FOO' = 'FOO'`. (This trick due to @geraldyeo and @fagerbua, in comments on facebook/flow#2377 -- thanks!) Do that, then. --- Made the edit with perl -i -0pe \ 's/export const ([A-Z_]+)\K =/: "$1" =/g' \ src/actionConstants.js and then manual fixup on the one exceptional case `REHYDRATE`, which Flow kindly pointed out. We had a whole bunch of type errors of the kind this hides; the bulk of them were for Event*Action types, and their fixes went into the parent commit. This commit adds one more missing action type which a number of reducers had type errors on, and a few other simple fixes. PS: A quick tip for reading this diff: `git log -p --patience`.
This is more of a question, than a bug report. Flow allows this:
It implies that inferred either
typeof
doesn't return the lowest possible, or that type ofx
isstring
, not'X'
, although literal value ofx
is preserved:The question is: why it is implemented this way? It makes impossible to define literal type and instance of that type without duplication:
The text was updated successfully, but these errors were encountered: