match

Symbol.match A regular expression method that matches the regular expression against a string. Called by the String.prototype.match method.

Value-based elements

Value-based elements Value based elements are simply looked up by identifiers that are in scope. import MyComponent from "./myComponent"; <MyComponent />; // ok <SomeOtherComponent />; // error It is possible to limit the type of a value-based element. However, for this we must introduce two new terms: the element class type and the element instance type. Given <Expr />, the element class type is the type of Expr. So in the example above, if MyComponent was an ES6 class the

Downloading

Downloading Getting type declarations in TypeScript 2.0 and above requires no tools apart from npm. As an example, getting the declarations for a library like lodash takes nothing more than the following command npm install --save @types/lodash

Best common type

Best common type When a type inference is made from several expressions, the types of those expressions are used to calculate a “best common type”. For example, let x = [0, 1, null]; To infer the type of x in the example above, we must consider the type of each array element. Here we are given two choices for the type of the array: number and null. The best common type algorithm considers each candidate type, and picks the type that is compatible with all the other candidates. Because the best

Constructor functions

Constructor functions When you declare a class in TypeScript, you are actually creating multiple declarations at the same time. The first is the type of the instance of the class. class Greeter { greeting: string; constructor(message: string) { this.greeting = message; } greet() { return "Hello, " + this.greeting; } } let greeter: Greeter; greeter = new Greeter("world"); console.log(greeter.greet()); Here, when we say let greeter: Greeter, we’re using Greeter as the type of

split

Symbol.split A regular expression method that splits a string at the indices that match the regular expression. Called by the String.prototype.split method.

Create a webpack configuration file

Create a webpack configuration file Create a webpack.config.js file at the root of the project directory. module.exports = { entry: "./src/index.tsx", output: { filename: "./dist/bundle.js", }, // Enable sourcemaps for debugging webpack's output. devtool: "source-map", resolve: { // Add '.ts' and '.tsx' as resolvable extensions. extensions: ["", ".webpack.js", ".web.js", ".ts", ".tsx", ".js"] }, module: { loaders: [ // All files with a '.ts' or '.tsx' ex

Schema

Schema Schema can be found at: http://json.schemastore.org/tsconfig

Module Augmentation

Module Augmentation Although JavaScript modules do not support merging, you can patch existing objects by importing and then updating them. Let’s look at a toy Observable example: // observable.js export class Observable<T> { // ... implementation left as an exercise for the reader ... } // map.js import { Observable } from "./observable"; Observable.prototype.map = function (f) { // ... another exercise for the reader } This works fine in TypeScript too, but the compiler doesn’t kn

Inferring the types

Inferring the types In playing with the example, you may notice that the TypeScript compiler can figure out the type if you have types on one side of the equation but not the other: // myAdd has the full function type let myAdd = function(x: number, y: number): number { return x + y; }; // The parameters 'x' and 'y' have the type number let myAdd: (baseValue:number, increment:number) => number = function(x, y) { return x + y; }; This is called “contextual typing”, a form of type inferen