1
Static Type Checking
With Flow JS
By Shivam Latawa
What is a Static Type Checker?
• Compile time issues
3
• Annotate variables, functions and components
• Makes you code faster, smarter and scalable
JS has a type system
• typeof x
4
• Number
• String
• Boolean
• Object
• Function
• Symbol
• AND …
• Undefined
• Null
JS has (runtime) type errors
• _ is not a function
5
• Cannot read property _ of null/undefined
What is Flow.js
• Flow is a static type checker for JS
6
• Helps to identify the issues/bugs in your code
• It will change the way you code in JS
• It makes you code smarter, faster and more productive
Real time error in code
7
How will that look with Flow?
8
Features of Flow
Types… Literal and Wrapper Types
Types… Null and Void Types
Types… Maybe Types
Types… Optional Object property and Functional parameters
Types… Mixed types
Types… Any Type
Types… Mixed vs Any Type
The difference is the "vice-versa": any can flow into other types
but mixed can not.
Types… Mixed vs Any Type
Any supports covariance and contravariance. It’s a super-type and a
subtype of all types.
Mixed supports covariance only. It's a super-type and not a sub type of all
types.
Types… Variable Types
Types… Array and Tuple Types
Types… Class Types
Types… Union Types
Types… Intersection Types
Types… Type Aliases
Types… typeof Type
Types… Utility Types
How to use Flow in your project?
/* @flow */ (add to the top of your file and make your project flow typed
incrementally)
npm install --save-dev flow-bin
npm install -g flow-bin (globally install)
flow init (will generate a .flowconfig file)
flow check (to check for the type errors)
Pros
● Code is more likely to work if it compiles, making you more productive
● When adding flowtypes to code it finds bugs
● Feels much safer making changes as a team
● Code is self documenting, makes code more readable
● Add flow gradually to the project.
Cons
• New syntax + esnext can be overwhelming
Flow vs Typescript
“We built Flow because we didn't see TypeScript going in the right direction
for the community. For example, it purposefully does not attempt to be a
sound type system… …documenting the API using types is useful (in fact
using Flow types you can generate API documentation using
documentation.js). But the point is that you don't need type annotations
everywhere in Flow to get something out of it. The places where you do add
type annotations Flow ends up doing a lot more work for you because it's
able to infer so much. If you aren't telling TypeScript what to do all the time,
it won't do anything for you. With TypeScript 2.0, it is starting to do more of
this, but it has a long way to go to match how much Flow is able to infer.”
Conclusions
• JavaScript doesn't give us enough type errors
• Static type checking can help to reduce the bugs in your
application
• Working with types will help you think better
References
● https://flow.org/en/docs/
● https://www.lullabot.com/articles/flow-for-static-type-checking-
javascript
Questions?
THANK YOU
Shivam Latawa
UI Developer, ThoughtWorks
slatawa@thoughtworks.com
@shivam_latawa

Static Type Checking with FlowJs