Skip to content

Commit aafe70f

Browse files
Merge branch 'master' into patch-1
2 parents 4549238 + c285f6b commit aafe70f

11 files changed

Lines changed: 229 additions & 30 deletions

File tree

‎pages/Advanced Types.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ function proxify<T>(o: T): Proxify<T> {
857857
let proxyProps = proxify(props);
858858
```
859859

860-
Note that `Readonly<T>` and `Partial<T>` are so useful, they are included in TypeScript's standard libarary along with `Pick` and `Record`:
860+
Note that `Readonly<T>` and `Partial<T>` are so useful, they are included in TypeScript's standard library along with `Pick` and `Record`:
861861

862862
```ts
863863
type Pick<T, K extends keyof T> = {

‎pages/Generics.md‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,21 +269,18 @@ loggingIdentity({length: 10, value: 3});
269269
## Using Type Parameters in Generic Constraints
270270

271271
You can declare a type parameter that is constrained by another type parameter.
272-
For example, here we'd like to take two objects and copy properties from one to the other.
273-
We'd like to ensure that we're not accidentally writing any extra properties from our `source`, so we'll place a constraint between the two types:
272+
For example, here we'd like to get a property from an object given its name.
273+
We'd like to ensure that we're not accidentally grabbing a property that does not exist on the `obj`, so we'll place a constraint between the two types:
274274

275275
```ts
276-
function copyFields<T extends U, U>(target: T, source: U): T {
277-
for (let id in source) {
278-
target[id] = source[id];
279-
}
280-
return target;
276+
function getProperty<T, K extends keyof T>(obj: T, key: K) {
277+
return obj[key];
281278
}
282279

283280
let x = { a: 1, b: 2, c: 3, d: 4 };
284281

285-
copyFields(x, { b: 10, d: 20 }); // okay
286-
copyFields(x, { Q: 90 }); // error: property 'Q' isn't declared in 'x'.
282+
getProperty(x, "a"); // okay
283+
getProperty(x, "m"); // error: Argument of type 'm' isn't assignable to 'a' | 'b' | 'c' | 'd'.
287284
```
288285

289286
## Using Class Types in Generics

‎pages/Interfaces.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ Variables use `const` whereas properties use `readonly`.
143143

144144
# Excess Property Checks
145145

146-
In our first example using interfaces, TypeScript let us pass `{ size: number; label: string; }` to something that only expected a `{ label: string; }`.
146+
In our first example using interfaces, TypeScript lets us pass `{ size: number; label: string; }` to something that only expected a `{ label: string; }`.
147147
We also just learned about optional properties, and how they're useful when describing so-called "option bags".
148148

149149
However, combining the two naively would let you to shoot yourself in the foot the same way you might in JavaScript.

‎pages/JSX.md‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ In order to use JSX you must do two things.
1212
1. Name your files with a `.tsx` extension
1313
2. Enable the `jsx` option
1414

15-
TypeScript ships with two JSX modes: `preserve` and `react`.
15+
TypeScript ships with three JSX modes: `preserve`, `react`, and `react-native`.
1616
These modes only affect the emit stage - type checking is unaffected.
1717
The `preserve` mode will keep the JSX as part of the output to be further consumed by another transform step (e.g. [Babel](https://babeljs.io/)).
1818
Additionally the output will have a `.jsx` file extension.
1919
The `react` mode will emit `React.createElement`, does not need to go through a JSX transformation before use, and the output will have a `.js` file extension.
20+
The `react-native` mode is the equivalent of `preserve` in that it keeps all JSX, but the output will instead have a `.js` file extension.
2021

21-
Mode | Input | Output | Output File Extension
22-
-----------|-----------|------------------------------|----------------------
23-
`preserve` | `<div />` | `<div />` | `.jsx`
24-
`react` | `<div />` | `React.createElement("div")` | `.js`
22+
Mode | Input | Output | Output File Extension
23+
---------------|-----------|------------------------------|----------------------
24+
`preserve` | `<div />` | `<div />` | `.jsx`
25+
`react` | `<div />` | `React.createElement("div")` | `.js`
26+
`react-native` | `<div />` | `<div />` | `.js`
2527

2628
You can specify this mode using either the `--jsx` command line flag or the corresponding option in your [tsconfig.json](./tsconfig.json.md) file.
2729

‎pages/Module Resolution.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Any other import is considered **non-relative**.
3333
Some examples include:
3434

3535
* `import * as $ from "jquery";`
36-
* `import { Component } from "angular2/core";`
36+
* `import { Component } from "@angular/core";`
3737

3838
A relative import is resolved relative to the importing file and *cannot* resolve to an ambient module declaration.
3939
You should use relative imports for your own modules that are guaranteed to maintain their relative location at runtime.
@@ -199,13 +199,13 @@ Value of *baseUrl* is determined as either:
199199

200200
Note that relative module imports are not impacted by setting the baseUrl, as they are always resolved relative to their importing files.
201201

202-
You can find more documentation on baseUrl in [RequireJS](http://requirejs.org/docs/api.html#config-baseUrl) and [SystemJS](https://github.com/systemjs/systemjs/blob/master/docs/overview.md#baseurl) documentation.
202+
You can find more documentation on baseUrl in [RequireJS](http://requirejs.org/docs/api.html#config-baseUrl) and [SystemJS](https://github.com/systemjs/systemjs/blob/master/docs/config-api.md#baseurl) documentation.
203203

204204
### Path mapping
205205

206206
Sometimes modules are not directly located under *baseUrl*.
207207
For instance, an import to a module `"jquery"` would be translated at runtime to `"node_modules\jquery\dist\jquery.slim.min.js"`.
208-
Loaders use a mapping configuration to map module names to files at run-time, see [RequireJs documentation](http://requirejs.org/docs/api.html#config-paths) and [SystemJS documentation](https://github.com/systemjs/systemjs/blob/master/docs/overview.md#map-config).
208+
Loaders use a mapping configuration to map module names to files at run-time, see [RequireJs documentation](http://requirejs.org/docs/api.html#config-paths) and [SystemJS documentation](https://github.com/systemjs/systemjs/blob/master/docs/config-api.md#paths).
209209

210210
The TypeScript compiler supports the declaration of such mappings using `"paths"` property in `tsconfig.json` files.
211211
Here is an example for how to specify the `"paths"` property for `jquery`.
@@ -264,15 +264,15 @@ Following this logic, the compiler will attempt to resolve the two imports as su
264264
* import 'folder1/file2'
265265
1. pattern '*' is matched and wildcard captures the whole module name
266266
2. try first substitution in the list: '*' -> `folder1/file2`
267-
3. result of substitution is relative name - combine it with *baseUrl* -> `projectRoot/folder1/file2.ts`.
267+
3. result of substitution is non-relative name - combine it with *baseUrl* -> `projectRoot/folder1/file2.ts`.
268268
4. File exists. Done.
269269
* import 'folder2/file3'
270270
1. pattern '*' is matched and wildcard captures the whole module name
271271
2. try first substitution in the list: '*' -> `folder2/file3`
272-
3. result of substitution is relative name - combine it with *baseUrl* -> `projectRoot/folder2/file3.ts`.
272+
3. result of substitution is non-relative name - combine it with *baseUrl* -> `projectRoot/folder2/file3.ts`.
273273
4. File does not exist, move to the second substitution
274274
5. second substitution 'generated/*' -> `generated/folder2/file3`
275-
6. result of substitution is relative name - combine it with *baseUrl* -> `projectRoot/generated/folder2/file3.ts`.
275+
6. result of substitution is non-relative name - combine it with *baseUrl* -> `projectRoot/generated/folder2/file3.ts`.
276276
7. File exists. Done.
277277

278278
### Virtual Directories with `rootDirs`

‎pages/Modules.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ export class SomeType { /* ... */ }
628628
export function someFunc() { /* ... */ }
629629
```
630630

631-
Conversly when importing:
631+
Conversely when importing:
632632

633633
### Explicitly list imported names
634634

‎pages/Variable Declarations.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ Most people expect the output to be
157157
Remember what we mentioned earlier about variable capturing?
158158
Every function expression we pass to `setTimeout` actually refers to the same `i` from the same scope.
159159

160-
Let's take a minute to consider that means.
160+
Let's take a minute to consider what that means.
161161
`setTimeout` will run a function after some number of milliseconds, *but only* after the `for` loop has stopped executing;
162162
By the time the `for` loop has stopped executing, the value of `i` is `10`.
163163
So each time the given function gets called, it will print out `10`!
@@ -472,7 +472,7 @@ function f([first, second]: [number, number]) {
472472
console.log(first);
473473
console.log(second);
474474
}
475-
f(input);
475+
f([1, 2]);
476476
```
477477

478478
You can create a variable for the remaining items in a list using the syntax `...`:

‎pages/declaration files/Do's and Don'ts.md‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ function reverse(s: String): String;
1717
function reverse(s: string): string;
1818
```
1919

20-
If you're tempted to use the type `Object`, consider using `any` instead.
21-
There is currently no way in TypeScript to specify an object that is "not a primitive".
22-
<!--(Revisit if/when #1809 is implemented)-->
20+
Instead of `Object`, use the non-primitive `object` type ([added in TypeScript 2.2](../release notes/TypeScript 2.2.md#object-type)).
2321

2422
## Generics
2523

‎pages/release notes/TypeScript 2.1.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ var Derived = (function (_super) {
452452
}(Base));
453453
```
454454

455-
> This change entails a break in the behavior of extending built-in classes like `Error`, `Array`, `Map`, etc.. Please see the [extending built-ins breaking change documnetation](https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work) for more details.
455+
> This change entails a break in the behavior of extending built-in classes like `Error`, `Array`, `Map`, etc.. Please see the [extending built-ins breaking change documentation](https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work) for more details.
456456
457457
# Configuration inheritance
458458

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
# Support for Mix-in classes
2+
3+
TypeScript 2.2 adds support for the ECMAScript 2015 mixin class pattern (see [MDN Mixin description](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Mix-ins) and ["Real" Mixins with JavaScript Classes](http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/) for more details) as well as rules for combining mixin construct signatures with regular construct signatures in intersection types.
4+
5+
##### First some terminology
6+
7+
A **mixin constructor type** refers to a type that has a single construct signature with a single rest argument of type `any[]` and an object-like return type. For example, given an object-like type `X`, `new (...args: any[]) => X` is a mixin constructor type with an instance type `X`.
8+
9+
A **mixin class** is a class declaration or expression that `extends` an expression of a type parameter type. The following rules apply to mixin class declarations:
10+
11+
* The type parameter type of the `extends` expression must be constrained to a mixin constructor type.
12+
* The constructor of a mixin class (if any) must have a single rest parameter of type `any[]` and must use the spread operator to pass those parameters as arguments in a `super(...args)` call.
13+
14+
Given an expression `Base` of a parametric type `T` with a constraint `X`, a mixin class `class C extends Base {...}` is processed as if `Base` had type `X` and the resulting type is the intersection `typeof C & T`.
15+
In other words, a mixin class is represented as an intersection between the mixin class constructor type and the parametric base class constructor type.
16+
17+
When obtaining the construct signatures of an intersection type that contains mixin constructor types, the mixin construct signatures are discarded and their instance types are mixed into the return types of the other construct signatures in the intersection type.
18+
For example, the intersection type `{ new(...args: any[]) => A } & { new(s: string) => B }` has a single construct signature `new(s: string) => A & B`.
19+
20+
##### Putting all of the above rules together in an example
21+
22+
```ts
23+
class Point {
24+
constructor(public x: number, public y: number) {}
25+
}
26+
27+
class Person {
28+
constructor(public name: string) {}
29+
}
30+
31+
type Constructor<T> = new(...args: any[]) => T;
32+
33+
function Tagged<T extends Constructor<{}>>(Base: T) {
34+
return class extends Base {
35+
_tag: string;
36+
constructor(...args: any[]) {
37+
super(...args);
38+
this._tag = "";
39+
}
40+
}
41+
}
42+
43+
const TaggedPoint = Tagged(Point);
44+
45+
let point = new TaggedPoint(10, 20);
46+
point._tag = "hello";
47+
48+
class Customer extends Tagged(Person) {
49+
accountBalance: number;
50+
}
51+
52+
let customer = new Customer("Joe");
53+
customer._tag = "test";
54+
customer.accountBalance = 0;
55+
```
56+
57+
Mixin classes can constrain the types of classes they can mix into by specifying a construct signature return type in the constraint for the type parameter.
58+
For example, the following `WithLocation` function implements a subclass factory that adds a `getLocation` method to any class that satisfies the `Point` interface (i.e. that has `x` and `y` properties of type `number`).
59+
60+
```ts
61+
interface Point {
62+
x: number;
63+
y: number;
64+
}
65+
66+
const WithLocation = <T extends Constructor<Point>>(Base: T) =>
67+
class extends Base {
68+
getLocation(): [number, number] {
69+
return [this.x, this.y];
70+
}
71+
}
72+
```
73+
74+
# `object` type
75+
76+
TypeScript did not have a type that represents the non-primitive type, i.e. any thing that is not `number`, `string`, `boolean`, `symbol`, `null`, or `undefined`. Enter the new `object` type.
77+
78+
With `object` type, APIs like `Object.create` can be better represented. For example:
79+
80+
```ts
81+
declare function create(o: object | null): void;
82+
83+
create({ prop: 0 }); // OK
84+
create(null); // OK
85+
86+
create(42); // Error
87+
create("string"); // Error
88+
create(false); // Error
89+
create(undefined); // Error
90+
```
91+
92+
# Support for `new.target`
93+
94+
The `new.target` meta-property is new syntax introduced in ES2015.
95+
When an instance of a constructor is created via `new`, the value of `new.target` is set to be a reference to the constructor function initially used to allocate the instance.
96+
If a function is called rather than constructed via `new`, `new.target` is set to `undefined`.
97+
98+
`new.target` comes in handy when `Object.setPrototypeOf` or `__proto__` needs to be set in a class constructor. One such use case is inheriting from `Error` in NodeJS v4 and higher.
99+
100+
##### Example
101+
102+
```ts
103+
class CustomError extends Error {
104+
constructor(message?: string) {
105+
super(message); // 'Error' breaks prototype chain here
106+
Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
107+
}
108+
}
109+
```
110+
111+
This results in the generated JS
112+
113+
```js
114+
var CustomError = (function (_super) {
115+
__extends(CustomError, _super);
116+
function CustomError() {
117+
var _newTarget = this.constructor;
118+
var _this = _super.apply(this, arguments); // 'Error' breaks prototype chain here
119+
_this.__proto__ = _newTarget.prototype; // restore prototype chain
120+
return _this;
121+
}
122+
return CustomError;
123+
})(Error);
124+
```
125+
126+
`new.target` also comes in handy for writing constructable functions, for example:
127+
128+
```ts
129+
function f() {
130+
if (new.target) { /* called via 'new' */ }
131+
}
132+
```
133+
134+
Which translates to:
135+
136+
```js
137+
function f() {
138+
var _newTarget = this && this instanceof f ? this.constructor : void 0;
139+
if (_newTarget) { /* called via 'new' */ }
140+
}
141+
```
142+
143+
# Better checking for `null`/`undefined` in operands of expressions
144+
145+
TypeScript 2.2 improves checking of nullable operands in expressions. Specifically, these are now flagged as errors:
146+
147+
* If either operand of a `+` operator is nullable, and neither operand is of type `any` or `string`.
148+
* If either operand of a `-`, `*`, `**`, `/`, `%`, `<<`, `>>`, `>>>`, `&`, `|`, or `^` operator is nullable.
149+
* If either operand of a `<`, `>`, `<=`, `>=`, or `in` operator is nullable.
150+
* If the right operand of an `instanceof` operator is nullable.
151+
* If the operand of a `+`, `-`, `~`, `++`, or `--` unary operator is nullable.
152+
153+
An operand is considered nullable if the type of the operand is `null` or `undefined` or a union type that includes `null` or `undefined`.
154+
Note that the union type case only only occurs in `--strictNullChecks` mode because `null` and `undefined` disappear from unions in classic type checking mode.
155+
156+
# Dotted property for types with string index signatures
157+
158+
Types with a string index signature can be indexed using the `[]` notation, but were not allowed to use the `.`.
159+
Starting with TypeScript 2.2 using either should be allowed.
160+
161+
```ts
162+
interface StringMap<T> {
163+
[x: string]: T;
164+
}
165+
166+
const map: StringMap<number>;
167+
168+
map["prop1"] = 1;
169+
map.prop2 = 2;
170+
171+
```
172+
173+
This only apply to types with an *explicit* string index signature.
174+
It is still an error to access unknown properties on a type using `.` notation.
175+
176+
# Support for spread operator on JSX element children
177+
178+
TypeScript 2.2 adds support for using spread on a JSX element children.
179+
Please see [facebook/jsx#57](https://github.com/facebook/jsx/issues/57) for more details.
180+
181+
##### Example
182+
183+
```ts
184+
function Todo(prop: { key: number, todo: string }) {
185+
return <div>{prop.key.toString() + prop.todo}</div>;
186+
}
187+
188+
function TodoList({ todos }: TodoListProps) {
189+
return <div>
190+
{...todos.map(todo => <Todo key={todo.id} todo={todo.todo} />)}
191+
</div>;
192+
}
193+
194+
let x: TodoListProps;
195+
196+
<TodoList {...x} />
197+
```
198+
199+
# New `jsx: react-native`
200+
201+
React-native build pipeline expects all files to have a `.js` extensions even if the file contains JSX syntax.
202+
The new `--jsx` value `react-native` will persevere the JSX syntax in the output file, but give it a `.js` extension.

0 commit comments

Comments
 (0)