Last active
July 10, 2018 12:34
-
-
Save karlhorky/fc90f1d0953004d2514c4508fe7a2f55 to your computer and use it in GitHub Desktop.
Predicate Function Checks Using Flow
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
// Interactive demo on Try Flow: https://flow.org/try/#0PTAEGEHsFsAcEMBOBTUAXAFgSwM4DoCAoAE2QGMAbJVAMwFcA7MtLSB0XAZTUSwYHNQACgBu8CnWQAuUDh59+AShk9JAbhLkqKUPSYs2HHN14DhYidNDwGAT2W7xOZBsJ7mrdl3lmh8RaAA3oSgoChodIjsaLawyJA01qAAvKmgAORypvzpGgC+hIQgoJxYcBSoZPDOOKAA7pCIANaEQt7ZQunw6Q6qyIoabcY+-EIAjA40Tv2uxQCyjZUwsBUAHqBVNaDEbOlo9Y1NUpqU1KBiiNYyAPxZCoPtCn6T0wOgxQCiiIiNwnxkjRQzEUhWKBDw8H48D4cnQ2BwxyKYAAQsg0GhkJcYnEyBhyE0FKA6DhCQBSXH4nBI0AAJWQNBkGHRsARIBoFEgdTwjX4wGQDGAOzIOGA2OQIvcBgYIoAxGhIGQALSwFDELBVDGKyWeKnawyPAQAJmeMnJeLITVqwVC4Ui0Vi8US8BSaUyI1yhAKbkYHkMAKY8DQfhudwEABpQAAjEMjByhwTWjiJIYmBTG-ygABkmaMqaNQkjigCiZtaLtSQA1FGNKECqWIlEMh68kA | |
// More information in this GitHub Issue: https://github.com/facebook/flow/issues/34 | |
// Compare this... | |
declare function isString (value: string): true; | |
declare function isString (value: any): false; | |
function isString (a) { | |
return typeof a === 'string'; | |
} | |
// Simple cases work | |
(isString('a'): true); | |
(isString(1): false); | |
// More complex cases don't work: | |
declare var a: ?string; | |
(isString(a): false); // Error (incorrect) | |
// ...against this: | |
// Better typechecking using %checks | |
// Ref: https://flow.org/en/docs/types/functions/#toc-predicate-functions | |
function isString2(a): %checks { | |
return typeof a === 'string'; | |
} | |
function concat(a: ?string, b: ?string): string { | |
if (isString2(a) && isString2(b)) { | |
return a + b; | |
} | |
return ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment