Christoffer Niska
const item = 'something';
const list = [item];
// is the same as:
const item: string = 'something';
const list: Array<string> = [item];Flow infers types automatically when you assign values to your variables.
type Something = string | number | null;
type AllowedActions = IncrementAction | DecrementAction;Useful when you want define types that accept multiple types.
type Action<Type: string, Payload> = {
type: Type,
payload: Payload
};Useful when you want to define dynamic types that you can reuse.
const takesOptionalType = (type: ?string) => {
...
}
type Driver = {
rating: ?number
}Useful when you want to define optional arguments or properties.
// anything will flow into any, but you loose type-safety
const takesAnything = (anything: any) => null
// mixed requires you to type-check before using the value
const takesMixed = (something: mixed) => {
if (typeof something === 'string') {
// ...
}
}
// asterisk (*) infers its type
class Driver {
rating: *;
constructor() {
this.rating = 5; // rating becomes a number
}
}...
// $FlowIgnore
export default connect(/* ... */)(Counter)
You will loose type-safety if you tell Flow to ignore your code.
Twitter, GitHub: @Crisu83