function sendMessage(to, message) { var _socket$send; if (!(to instanceof User)) throw new TypeError("Value of argument 'to' violates contract."); if (typeof message !== "string") throw new TypeError("Value of argument 'message' violates contract."); _socket$send = socket.send(to, message); if (typeof _socket$send !== "boolean") throw new TypeError("Function 'sendMessage' return value violates contract."); return _socket$send;}
And guards against some silly mistakes, for example the following code will fail to compile with a SyntaxError, because the function can return the wrong type.
function foo (): boolean { if (Math.random() > 0.5) { return "yes"; // <-- SyntaxError - string is not boolean } else { return false; }}function bar (input: string = 123): string { // <-- SyntaxError: default value is not string return input + "456";}
The example configuration will disable typecheck when NODE_ENV=production which is usually preferable for performance reasons.
Important: This plugin has a dependency on babel-plugin-syntax-flow and babel-plugin-transform-flow-strip-types.
Without syntax-flow, babel will be unable to parse the flow annotation syntax.
Without transform-flow-strip-types, the type annotations will be included in the output which will make it unparsable by JS engines.
If you are not already using the babel-preset-react plugin, you must install those plugins and include them in your babel configuration (usually .babelrc). Put them aftertypecheck in the list, e.g.
If you are using babel-preset-react you can ignore this warning.
Note Depending on your babel configuration you may encounter issues where typecheck interferes with other transformations. This can almost always be fixed by adjusting your preset order and setting "passPerPreset": true in your .babelrc.
import type { CsvDataType, LocationType} from './places';// You can now use CsvDataType and LocationType just like any other type.
Note that in contrast to flow, an imported type must be an actual type and cannot be a class or other concrete value.
Optimization
In cases where typecheck can statically verify that the return value is of the correct type, no type checks will be inserted, for instance:
function bar (): string|Object { if (Math.random() > 0.5) { return "yes"; } else { return { message: "no" }; }}
will produce no type checks at all, because we can trivially tell that the function can only return one of the two permitted types.
This is also true for simple cases like:
function createUser (): User { return new User(); // <-- no typecheck required}
This is currently quite limited though, as the plugin can only statically infer the types of literals and very simple expressions, it can't (yet) statically verify e.g. the result of a function call. In those cases a runtime type check is required:
function createUser (): User { return User.create(); // <-- produces runtime typecheck}
function demo (input: string[]): number { return input.length;}demo(["a", "b", "c"]); // okdemo([1, 2, 3]); // TypeError
Shape tracking
type User = { name: string; email: string;};function demo (input: User): string { return input.name;}demo({}); // TypeErrordemo({name: 123, email: "test@test.com"}); // TypeErrordemo({name: "test", email: "test@test.com"}); // ok
Pragmas
Sometimes you might need to disable type checking for a particular file or section of code.
To ignore an entire file, add a comment at the top level scope of the file:
Note: Because of how typecheck works, it's not possible to ignore individual lines, only entire statements or files.
So if you ignore e.g. an if statement, the entire body of that statement will be ignored.
You can also control the disabling and enabling of type checking using the plugin options and the @typecheck pragma. Type checking will be enabled only for files where any of the configured only values are found in the @typecheck pragma. With babel configuration: