Both TypeScript and Flow are very similar products and they share most of their syntax with some important differences. In this document I've tried to compile the list of differences and similarities between Flowtype and TypeScript -- specifically the syntax, usage and usability.
This document might be incomplete and/or contain mistakes and was last updated to describe TypeScript 3.7.0 and Flow 0.101.
I'm maintaining it in my spare time, so if you find mistakes, or learn about latest additions to either project, please help keep this repo up-to-date by contributing and editing this page.
Thanks!
Some of these differences are subjective (e.g. error readability), and I'd love to make this as scientific as possible — so please contribute to make it better. :)
TypeScript | Flow | |
---|---|---|
Leading Design Goal / North Star | identify errors in programs through a balance between correctness and productivity | enforce type soundness / safety |
IDE integrations | top-notch: language server, built-in refactorings, type and typedoc information on hover, snappy go-to-definition | language server is a work in progress, some IDEs use the CLI and require saving the file to run the type-check, refactorings in alpha, only type information on hover, sketchy go-to-definition |
type-checking speed (excluding transpilation) | benchmarks needed | benchmarks needed, in-depth description |
autocomplete |
|
|
expressiveness | great (since TS @ 2.1) | great |
type safety | very good (7 / 10) | great (8 / 10) |
specifying generic parameters during call-time (f<T>(x) ) |
yes e.g. | yes (since Flow 0.72) |
specifying generic parameters for type definitions | yes | yes |
typings for public libraries | plenty of well maintained typings | a handful of mostly incomplete typings |
unique features |
|
|
type spread operator | shipped > 3.2rc | shipped >=0.42 |
support for nullish coalescing proposal | shipped > 3.7beta | yes |
support for decorators proposal | yes, legacy proposal | only parsing of legacy proposal, no type-checking |
support for extending built-in types | yes | no |
userland plugins | basic, not effecting emitting yet (planned) | no |
programmatic hooking | architecture prepared, work in progress | work in progress |
documentation and resources |
|
|
ease-of-understanding of errors | good | good in some, vague in other cases |
transparency | meeting notes, leadership reasoning and roadmap happens mostly publicly | low transparency, roadmap developed behind closed doors |
commercial support | no | no |
nominal and structural typing | structural | mostly structural, nominal for classes and imported opaque type aliases |
dynamic import types | import('module-name') since 2.9 |
undocumented $Exports<'module-name'> |
utility size (not emitted JavaScript) (latest version) |
function fooGood<T: { x: number }>(obj: T): T {
console.log(Math.abs(obj.x));
return obj;
}
function fooGood<T extends { x: number }>(obj: T): T {
console.log(Math.abs(obj.x));
return obj;
}
https://flow.org/blog/2015/03/12/Bounded-Polymorphism/
let a: ?string
// equivalent to:
let a: string | null | void
let a: string | null | undefined
Optional parameters implicitly add undefined
:
function f(x?: number) { }
// is semantically the same as:
function f(x: number | undefined) { }
// and also same as (the `| undefined` is redundant):
function f(x?: number | undefined) { }
Optional properties implicitly add undefined
class A {
foo?: string;
}
(1 + 1 : number);
(1 + 1) as number;
// OR (old version, not recommended):
<number> (1 + 1);
.flowconfig
[options]
module.name_mapper='^\(.*\)\.css$' -> '<PROJECT_ROOT>/CSSModule.js.flow'
CSSModule.js.flow
// @flow
// CSS modules have a `className` export which is a string
declare export var className: string;
declare module "*.css" {
export const className: string;
}
By default objects in Flow are not exact, i.e. they can contain more properties than declared, whereas in TypeScript they are always exact (must contain only declared properties). In future versions Flow plans to change this and make objects exact by default.
When using flow, { name: string }
only means “an object with at least a name property”.
type ExactUser = {| name: string, age: number |};
type User = { name: string, age: number };
type OptionalUser = $Shape<User>; // all properties become optional
TypeScript is more strict here, in that if you want to use a property which is not declared, you must explicitly say so by defining the indexed property. It is possible to use dotted syntax to access indexed properties since TypeScript 2.2. This is mostly a design decision as it forces you to write the typings upfront.
type ExactUser = { name: string, age: number };
type User = { name: string, age: number, [otherProperty: string]: any };
type OptionalUser = Partial<ExactUser>; // all properties become optional
import type {UserID, User} from "./User.js";
// equivalent:
import {type UserID, type User} from "./User.js";
TypeScript does not treat Types in any special way when importing.
import {UserID, User} from "./User.js";
Works the same in both cases, however Flow has an additional syntax to directly import a typeof
:
import typeof {jimiguitar as GuitarT} from "./User";
// OR
import {typeof jimiguitar} from "./User.js";
type GuitarT = jimiguitar;
// OR (below also works in TypeScript)
import {jimiguitar} from "./User.js";
type GuitarT = typeof jimiguitar;
import {jimiguitar} from "./User";
type GuitarT = typeof jimiguitar;
When you don't know a type, commonly you would use any
type. A restrictive type accepts anything, like any
but in order to use that variable you must ensure values type by refining it.
mixed
function stringifyNum(num: number) {
// Do stuff
}
function stringify(value: mixed) {
if (typeof value === 'string') {
return '' + value; // Works!
}
if (typeof value === 'number') {
return stringifyNum(value); // Works!
}
return '';
}
Reference: https://flow.org/en/docs/types/mixed/
unknown
function stringifyNum(num: number) {
// Do stuff
}
function stringify(value: unknown) {
if (typeof value === 'string') {
return '' + value; // Works!
}
if (typeof value === 'number') {
return stringifyNum(value); // Works!
}
return '';
}
Reference: https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#new-unknown-top-type
Classes are typed, so you don't need to define an explicit type for them. If you want to reference the type, you can do it the following way:
class Test {};
type TestType = typeof Test;
const instance = new Test();
type TestTypeFromInstance = Class<typeof instance>;
class Test {};
type TestType = typeof Test;
var props = {
foo: 1,
bar: 'two',
baz: 'three',
}
type PropsType = typeof props;
type KeysOfProps = $Enum<PropsType>;
function getProp<T>(key: KeysOfProps): T {
return props[key]
}
var props = {
foo: 1,
bar: 'two',
baz: 'three',
}
type PropsType = typeof props
type KeysOfProps = keyof PropsType;
function getProp<T>(key: KeysOfProps): T {
return props[key]
}
type $Record<T, U> = {[key: $Enum<T>]: U}
type SomeRecord = $Record<{ a: number }, string>
type SomeRecord = Record<{ a: number }, string>
type A = {
thing: string
}
// when the property is a string constant use $PropertyType (i.e. you know it when typing)
type lookedUpThing = $PropertyType<A, 'thing'>
// when you want the property to be dynamic use $ElementType (since Flow 0.49)
function getProperty<T : Object, Key : string>(obj: T, key: Key): $ElementType<T, Key> {
return obj[key];
}
Reference:
- facebook/flow#2952 (comment)
- https://github.com/facebook/flow/commit/968210c5887b5bdd47d17167300033d1e1077d1a
- facebook/flow#2464 (comment)
- flow/try
Arguably, it's a bit easier to type both cases in TS, since they follow the same pattern.
type A = {
thing: string
}
type lookedUpThing = A['thing']
// and...
function getProperty<T, K extends keyof T>(obj: T, key: K) {
return obj[key]; // Inferred type is T[K]
}
function setProperty<T, K extends keyof T>(obj: T, key: K, value: T[K]) {
obj[key] = value;
}
Reference:
These are functions that return a boolean, performing some logic to assert that a given input parameter is of a certain type.
The implementations differ between Flow and TypeScript:
In TypeScript, it ensures the mapping between: true
and value is T
, versus in the case of Flow, it ensures the value is "checked" against the logic within the body of the function (i.e. things like typeof
, instanceof
, value === undefined
).
This means you cannot tell Flow that the tested parameter is of an arbitrary type, which closes the door to complex cases, e.g.:
- reusing logic from a different function
- library definitions, where there is no body at all (it is possible to specify the body in the declaration, however you are still limited by the type of assertions you may specify)
function isNil(value: mixed): boolean %checks {
return value == null;
}
const thing = null;
if (!isNil(thing)) {
const another = thing.something;
}
Reference:
The current implementation in Flow is incomplete, which means you cannot yet use %checks
in class methods.
Example showing the limitation in the respective playgrounds: TypeScript vs Flow
Type-narrowing functions are called type guard functions in TypeScript.
function isNil<T>(value: T | null): value is null {
return value == null;
}
const thing: any = null;
if (!isNil(thing)) {
const another = thing.something;
}
$Call
utility type:
type Fn1 = <T>(T) => T;
type E = $Call<Fn1, number>;
declare var e: E; // E is number
(42: E); // OK
Reference: https://github.com/facebook/flow/commit/ac7d9ac68acc555973d495f0a3f1f97758eeedb4
ReturnType
utility type:
type fn1<T> = (a: T) => T;
type E = ReturnType<fn1<number>>;
var e: E; // E is number
type InputType = { hello: string };
type MappedType = $ObjMap<InputType, ()=>number>;
Reference:
- https://gist.github.com/gabro/bb83ed574690645053b815da2082b937
- https://twitter.com/andreypopp/status/782192355206135808
A bit more flexibility here, as you have access to each individual key name and can combine with Lookup types and even do simple transformations.
type InputType = { hello: string };
type MappedType = {
[P in keyof InputType]: number;
};
It is possible to declare multiple signatures for the same method (also called: overloading). This feature is undocumented, and only available in type declarations (.js.flow
files or module statements), not inline/alongside your code.
declare function add(x: string, y: string): string;
declare function add(x: number, y: number): number;
declare class Adder {
add(x: string, y: string): string;
add(x: number, y: number): number;
}
However, it's possible to create function overloads inline for functions outside of classes, by using additional declarations.
declare function add(x: string, y: string): string;
declare function add(x: number, y: number): number;
function add(x, y) {
return x + y;
}
add(1, 1); // Ok
add("1", "1"); // Ok
add(1, "1"); // Error
It is also possible to create function overloads using callable property syntax, see the section Object callable property.
TypeScript supports both function and method overloading, in both: type definitions (.d.ts
) and inline alongside code.
class Adder {
add(x: string, y: string): string;
add(x: number, y: number): number;
add(x, y) {
return x + y;
}
}
function add(x: string, y: string): string;
function add(x: number, y: number): number;
function add(x, y) {
return x + y;
}
type A = {
+b: string
}
let a: A = { b: 'something' }
a.b = 'something-else'; // ERROR
type A = {
readonly b: string
}
let a: A = { b: 'something' }
a.b = 'something-else'; // ERROR
One caveat that makes TypeScript's readonly
less safe is that the same non-readonly
property in a type is compatible with a readonly
property. This essentially means that you can pass an object with readonly
properties to a function which expects non-readonly properties and TypeScript will not throw errors: example.
empty
function returnsImpossible() {
throw new Error();
}
// type of returnsImpossible() is 'empty'
never
function returnsImpossible() {
throw new Error();
}
// type of returnsImpossible() is 'never'
type C = $Diff<{ a: string, b: number }, { a: string }>
// C is { b: number}
It only works properly as lower bound, i.e. you can assign something to it, but can't use it after that.
(source)
Flow also has $Rest<>
, which represents the result of the JS object rest operator ({ ...rest }
).
type Props = { name: string, age: number };
const props: Props = {name: 'Jon', age: 42};
const {age, ...otherProps} = props;
(otherProps: $Rest<Props, {|age: number|}>);
otherProps.age; // Error, since we removed it
You can define your own filter type, but it does not have a helper type for that.
class A {
a: string;
b: number;
}
class B {
a: string;
c: boolean;
}
type Omit<T, U> = Pick<T, Exclude<keyof T, keyof U>>;
//
type C = Omit<A, B>;
// C is { b: number }
However, Flow implementation is stricter in this case, as B have a property that A does not have, it would rise an error. In Typescript, however, they would be ignored.
Most of the syntax of Flow and TypeScript is the same. TypeScript is more expressive for certain use-cases (advanced mapped types with keysof, readonly properties), and Flow is more expressive for others (e.g. $Diff
).
The basic syntax are the same, except Flow has special syntax for the internal call property slot.
Both can be used to annotate function statics.
You can use objects with callable properties as functions: Try Flow
type F = {
(): string
};
const f: F = () => "hello";
const hello: string = f();
An overloaded function is a function with multiple call signatures. This is supported by Flow. And we list out the different syntaxes here: Try Flow
type F = {
(): string,
[[call]]: (number) => string,
[[call]](string): string
}
const f: F = (x?: number | string) => {
return x ? x.toString() : '';
}
Use call property to annotate function statics: Try Flow
type MemoizedFactorialType = {
cache: {
[number]: number,
},
[[call]](number): number,
}
const factorial: MemoizedFactorialType = n => {
if (!factorial.cache) {
factorial.cache = {}
}
if (factorial.cache[n] !== undefined) {
return factorial.cache[n]
}
factorial.cache[n] = n === 0 ? 1 : n * factorial(n - 1)
return factorial.cache[n]
}
Reference:
- Callable Objects
- immer.js uses it to overload the
produce
(default export) function which has multiple call signatures - Styled Components uses it to separate cases of being called on a string and wrapping a component
- Reselect Library Definition contains massive chunks of overloaded call properties
You can use objects with callable properties as functions: TypeScript Playground
type F = {
(): string;
}
const foo: F = () => "hello";
const bar: string = foo();
An overloaded function is a function with multiple call signatures. This is also supported by TypeScript: TypeScript Playground
type F = {
(): string,
(x: number): string,
(x: string): string
}
const f: F = (x?: number | string) => {
return x ? x.toString() : '';
}
Use call property to annotate function statics: TypeScript Playground
type MemoizedFactorialType = {
cache?: {
[n: number]: number,
},
(n: number): number,
}
const factorial: MemoizedFactorialType = n => {
if (!factorial.cache) {
factorial.cache = {}
}
else if (factorial.cache[n] !== undefined) {
return factorial.cache[n]
}
factorial.cache[n] = n === 0 ? 1 : n * factorial(n - 1)
return factorial.cache[n]
}
Reference:
The syntax in either tool is the same - question mark: ?
suffixing the parameter name:
function(a?: string) {}
In TypeScript and Flow (since version 0.72) you may use specify the type of a generic when calling the generic function or the constructor.
const set = new Set<string>();
Or using a more complex behavior:
function makeTgenerator<T>() {
return function(next: () => T) {
const something = next();
return something;
}
}
const usage = makeTgenerator<string>()
// 'usage' is of type: (next: () => string) => string
function something(this: { hello: string }, firstArg: string) {
return this.hello + firstArg;
}
class SomeClass {
constructor(public prop: string, private prop2: string) {
// transpiles to:
// this.prop = prop;
// this.prop2 = prop2;
}
private prop3: string;
}
Add !
to signify we know an object is non-null.
// Compiled with --strictNullChecks
function validateEntity(e?: Entity) {
// Throw exception if e is null or invalid entity
}
function processEntity(e?: Entity) {
validateEntity(e);
let s = e!.name; // Assert that e is non-null and access name
}
type XorY<T, U> = T extends U ? X : Y;
This alone, introduces new helper types, or types aliases.
type Exclude<T, U> = T extends U ? never : T;
/**
* Extract from T those types that are assignable to U
*/
type Extract<T, U> = T extends U ? T : never;
/**
* Exclude null and undefined from T
*/
type NonNullable<T> = T extends null | undefined ? never : T;
/**
* Obtain the return type of a function type
*/
type ReturnType<T extends (...args: any[]) => any> =
T extends (...args: any[]) => infer R ? R : any;
You can use +
and -
operators to modify mapped types.
type Mutable<T> = {
-readonly [P in keyof T]: T[P]
}
interface Foo {
readonly abc: number;
}
// 'abc' is no longer read-only.
type TotallyMutableFoo = Mutable<Foo>
Required
is a type mapper to make all properties of an object to be required.
Partial
is a type mapper to make all properties of an object to be optional.
Readonly
is a type mapper to make all properties of an object to be readonly.
*
as a type or a generic parameter signifies to the type-checker to infer the type if possible
Array<*>
However this type was deprecated in Flow 0.72.
https://flow.org/en/docs/lang/variance/
function getLength(o: {+p: ?string}): number {
return o.p ? o.p.length : 0;
}
Bivariance is among the design decisions driving TypeScript.
- microsoft/TypeScript#1265
- Undocumented Flow modifiers facebook/flow#2464
- http://sitr.us/2015/05/31/advanced-features-in-flow.html