Generics

Generics Don’t ever have a generic type which doesn’t use its type parameter. See more details in TypeScript FAQ page.

Generics

Generics Because TypeScript is a structural type system, type parameters only affect the resulting type when consumed as part of the type of a member. For example, interface Empty<T> { } let x: Empty<number>; let y: Empty<string>; x = y; // okay, y matches structure of x In the above, x and y are compatible because their structures do not use the type argument in a differentiating way. Changing this example by adding a member to Empty<T> shows how this works: interfac

Functions with overloads

Functions with overloads When a function has overloads, each overload in the source type must be matched by a compatible signature on the target type. This ensures that the target function can be called in all the same situations as the source function.

Function Types

Function Types Interfaces are capable of describing the wide range of shapes that JavaScript objects can take. In addition to describing an object with properties, interfaces are also capable of describing function types. To describe a function type with an interface, we give the interface a call signature. This is like a function declaration with only the parameter list and return type given. Each parameter in the parameter list requires both name and type. interface SearchFunc { (source: st

Function Parameter Bivariance

Function Parameter Bivariance When comparing the types of function parameters, assignment succeeds if either the source parameter is assignable to the target parameter, or vice versa. This is unsound because a caller might end up being given a function that takes a more specialized type, but invokes the function with a less specialized type. In practice, this sort of error is rare, and allowing this enables many common JavaScript patterns. A brief example: enum EventType { Mouse, Keyboard } in

Function declarations

Function declarations Destructuring also works in function declarations. For simple cases this is straightforward: type C = {a: string, b?: number} function f({a, b}: C): void { // ... } But specifying defaults is more common for parameters, and getting defaults right with destructuring can be tricky. First of all, you need to remember to put the type before the default value. function f({a, b} = {a: "", b: 0}): void { // ... } f(); // ok, default to {a: "", b: 0} Then, you need to rememb

for...of statements

for..of statements for..of loops over an iterable object, invoking the Symbol.iterator property on the object. Here is a simple for..of loop on an array: let someArray = [1, "string", false]; for (let entry of someArray) { console.log(entry); // 1, "string", false }

Find and Install Declaration Files

Find and Install Declaration Files For JavaScript library users, the Consumption section offers a few simple steps to locate and install corresponding declaration files.

Factories

Decorator Factories If we want to customize how a decorator is applied to a declaration, we can write a decorator factory. A Decorator Factory is simply a function that returns the expression that will be called by the decorator at runtime. We can write a decorator factory in the following fashion: function color(value: string) { // this is the decorator factory return function (target) { // this is the decorator // do something with 'target' and 'value'... } } NOTE You can see a more

Extending Interfaces

Extending Interfaces Like classes, interfaces can extend each other. This allows you to copy the members of one interface into another, which gives you more flexibility in how you separate your interfaces into reusable components. interface Shape { color: string; } interface Square extends Shape { sideLength: number; } let square = <Square>{}; square.color = "blue"; square.sideLength = 10; An interface can extend multiple interfaces, creating a combination of all of the interfaces.