-
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
Document "%checks" (Was: "if" condition type guard does not work when moved to function) #4723
Comments
Add function isMyFunction (data: mixed): boolean %checks { |
This leads to a question and a problem: Question: Is this documented somewhere? Problem: Since I'm not the only one who has never heard of that annotation Webstorm goes berserk (and all yellowish "I don't recognize what you are writing" background). Which leads back to the question - I don't want to file a Webstorm ticket for a feature that isn't documented. |
I agree it should be documented, but to work around your problem you can do this: function isMyFunction (data: mixed)/*: boolean %checks */{ |
@vkurchatkin I changed the title, would you please reopen the issue and label it "documentation"? I think that's better than me opening another issue. Could somebody tell me if |
UPDATE: The check does not work when I move it into another file (module) and |
It's expected. Only type annotations passes through files. This makes flow fast. Passing additional information with |
@TrySound There is a problem - so I opened an issue. |
@TrySound I read that yesterday. I searched for "%checked" in the issues list. I refrained from linking it because it isn't about |
Link: #4900 ‘When annotating with |
@panagosg7 since you implemented this could you tell me how I would use e.g. declare module "lodash" {
declare class Lodash {
isString: (value: any): boolean %checks (typeof value === 'string');
}
declare var exports: Lodash;
}
function method(x: string | number): number {
if (lodash.isString(x)) {
return x.charCodeAt(0);
} else {
return x;
}
} |
I'm afraid function predicates (and the use of |
@Whoaa512 if import the check function by name: import {isString} from 'lodash';
function method(x: string | number): number {
if (isString(x)) { .... }
} It works for me. |
@loyd Is that using the flow-typed declarations? or a custom one you wrote? Could you share that it looks like? |
I have tried not with lodash, but the same case: |
Permanent links to the code linked in the previous comment: Declaration, Import, Usage |
Thanks, my mistake =( |
Do predicate functions work with |
Checks is documented here. |
@panagosg7 Do you know if function predicates support was added for class methods? |
@akoppela No, class methods are still not supported. |
How would you use checks to say that a function is able to determine of an object is of certain type? A type you defined yourself like |
Description
I moved the boolean "if" condition into a function because it was pretty long. Without any other changes, this if-condition type guard is no longer recognized and Flow complains about using properties that that function checks exist.
After some experiments, I don't see a way to have a type guard be a function?
I understand the "mutability" assumption Flow makes whenever a function is called, but being unable to use a function as type guard, as seems to be the case, seems a little... severe. After all, functions starting with
is...()
(isBoolean, isThis, isThat, etc.) are quite common. I am unable to use a function in a type guard check?Code
Error
Working example
Flow Try link
The text was updated successfully, but these errors were encountered: