typeof 'str'; // 'string'
const variable = 3;
typeof variable; // 'number'
let variable; // What will the type be? ¯\_(ツ)_/¯
// `value` is a 'number'
const value: number = 3;
// `add` takes two 'number' arguments
// and returns a number
function add(a: number, b: number): number {
return a + b;
}
TypeScript & Flow
// number
const a: number = 3;
// string
const b: string = 'foo'
// boolean
const c: boolean = true;
// null
const d: null = null;
// void (undefined)
const e: void = undefined;
// In functions
function foo(
a: string = defaultValue,
b?: number // optional parameter
): boolean {} // return type
// Objects
const object
: {a: string, b: number}
= {a: "foo", b: 0};
// Destructuring
const [a: number] = bar;
const {a: number = 2} = bar;
let bar: any;
const foo: any = bar;
foo.z()[0].bar.yolo + '!!!'; // No checks
function parseInt(value: mixed): string {
if (typeof value === 'string') {
return value;
}
if (typeof value === 'number') {
return String(value);
}
return '0';
}
Modules without types will be considered as type any)
function readFile(path: string, cb: Function): void {
}
// More precise
function readFile(
path: string,
cb: (err: Error, content: string) => void
): void {
}
const obj: Object = {};
obj.foo; // considered as 'any'
// Loose (can have additional properties)
const obj
: { foo: string
, bar?: number // bar is optional
}
= { foo: 'yolo'
, bar: 123
, baz: true
};
// Strict (can't have additional properties) using {| |}
const obja
: {| foo: string
, bar?: number // bar is optional
|}
= { foo: 'yolo'
, bar: 123
, baz: true // Not allowed
};
const numberArray: Array<number> = [1, 2, 3]; // Also available as number[]
const userP: Promise<Object> = getUser('Jeroen');
// user.js
type UserId = string;
export type User = {|
id: UserId,
name: string
|};
function getUser(id: UserId): Promise<User> {
// ...
}
// greeting.js
import type {User} from './user';
function sayHiToUser(user: User): string {
return `Hello ${user.name}`;
}
let PORT: string | number = '3000';
PORT = 3000;
type User =
| AnonymousUser
| RegisteredUser
;
type CardColor =
| 'Heart'
| 'Clover'
| 'Diamond'
| 'Spade'
;
type Card = {
color: CardColor,
number: number
};
const card: Card = {
color: 'Spade',
number: 1
};
type Foo = {a: string} & {b: string}
const foo: Foo = {a: 'foo', b: 'bar'};
import type {Request} from 'express';
type CoorpRequest =
& Request
& { logger: Function
, currentUser: User
}
type User = {|
id: UserId,
name: string,
// ...
|};
type Discipline = {|
ref: string,
name: string,
modules: Module[]
// ...
|};
type Props = {
rating: ?number,
maxRating: ?number,
linkBuy: ?string,
linkTry: ?string,
author: {
name: ?string,
socialLinks: SocialLinks[]
}
};
const DisciplineRightaside = (
props: Props,
// Tuple: array of size 0 (no children)
children: []
) => {
}
const conditions = checker.shape({
props: checker.shape({
rating: checker.number.optional,
maxRating: checker.number.optional,
linkBuy: checker.string.optional,
linkTry: checker.string.optional,
author: checker.shape({
name: checker.string.optional,
socialLinks: checker.array.optional
}).optional
}),
children: checker.none
});
const initialModel: Model = 0;
const update = (
model: Model = initialModel,
action: Action
): Model => {
switch (action.type) {
case 'increment': {
return model + 1;
}
case 'decrement': {
return model - 1;
}
case 'reset': {
return 0;
}
}
return model;
};
type Model = number;
type Increment = {|
type: 'increment'
|};
type Decrement = {|
type: 'decrement'
|};
type Reset = {|
type: 'reset'
|};
type Action =
| Increment
| Decrement
| Reset
;
If you want to change your model, Flow will tell you where you missed stuff
Richard Feldman - "Making Impossible States Impossible"
type Survey = {
questions: Array<string>,
answers: Array<?string>
};
const survey: Survey = {
questions: [
"What is the color of Henry IV's white horse?"
],
answers: [
'White',
'Barack Obama'
]
}
// WTF happened here?
type Question = {
question: string,
answer: ?string
};
type Survey = Array<Question>;
const survey: Survey = [
{
question: "What is the color of Henry IV's white horse?"
answer: 'White'
}
];