-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start
A helper library link runtime type check to static flow check.
Assume you have a resolver, which the passed args is out of static type check.
const your_resolver = (src) => {
// .... doing something
return src;
});
to check it,just wrap it by check functions.
like: sourceCheck(sourceCaster, resolverToCheck) .resolverToCheck is your old resolver .And sourceCaster is a type caster(which do dynamic check for args,and link them to flow type.)
const dynamicResolver = sourceCheck(
(src) => ({
mail:pro.isString.isEmail(src.mail) // check src.mail .
}), (src) => {
// .... doing something
return src;
});
Like React,you only need to test the field will be used. As above,we test mail,so the type checked src pass to your old resolver is {mail:string}(with isEmail ensuring).
Let's test it:
function test() {
const user = {
id:'aaa',
name:'tom',
age:28,
mail:'xx@mail.com'// will pass, and 'xx@mail' will throw a error.
};
const data = user;
const rt = dynamicResolver(data, {}, {}, ({}:any));
return rt;
}
if we passed some data in, the resolver will also receive a Flow typed checked {mail:string}

no matter what type the passed in data is, the sourceCheck will always guaranteed the resolver receive the right data, or will throw a error to point the passed in data is wrong type.
And also sourceCheck marked the dynamic check to Flow,So if you use sourceCheck check the args,Flow will receive the type which you ensure must be.
As you can see,The checked src passed into resolver, will always corresponding with the sourceCaster.
const unknown:any = user;
const rt = resolver( unknown, {}, {}, ({}:any)); //will receive a {mail:string}
const rt = resolver( null, {}, {}, ({}:any)); // will failed