Javascript for control freaks
My recommendation
(I stole this slide from here)
The goal is to:
Write bug-resistant code
Write more robust code
Make it easier to read
Make it faster to write
Bugs prevented + understandable code > time it takes to tag stuff
Code will run even with type errors
Forces nullchecking
Only checks files tagged with @flow
Assumes you have no idea what you’re doing \o/ (not true for typescript)
You gotta actually tag stuff (but if you don’t, nbd)
Extra compile step (babel plugin for that)
Writing your own type classes just for flow
This can be kind of a pain, but is better in the long run
Forces you to actually think about your objects and try to conform with other files instead of just making up whatever you want as you go
x: number
x: string
x: boolean
x: null
x: void // typeof === ‘undefined’
x: mixed // I have thought about this, and you can use a variety of types
x: any // I may or may not have thought about this and idk what type ¯\_(ツ)_/¯
Built-in types
x: Function
x: Object // IT’S SOMETHING.. Not sure what. Or I can’t be bothered.
x: Response // for HTTP stuff
x: Promise // for json returns
x: MyCoolObject // for types you've defined
Other common types
Basic example
<- flow cries :(
variables have annotations
function returns have annotations
@flow means flow looks @ this
Nullable
Optional
Unions
(x: this | that)
Unions
OOO FANCY!
Making types
Making types
Generics
You can go Java levels of crazy with this
$> npm install --save-dev flow-bin
$> flow
Third party libraries
Solutions:
Third party libraries
Going with option 2 can lead down a rabbit hole of crazy and is not recommended (by me) 🙃