You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: pages/release notes/TypeScript 2.1.md
+81-81Lines changed: 81 additions & 81 deletions
Original file line number
Diff line number
Diff line change
@@ -235,87 +235,6 @@ let { z, ...obj1 } = obj;
235
235
obj1; // {x: number; y:number};
236
236
```
237
237
238
-
# Improved `any` Inference
239
-
240
-
Previously, if TypeScript can't figure out the type of a variable, it will choose the `any` type.
241
-
242
-
```ts
243
-
let x; // implicitly 'any'
244
-
let y = []; // implicitly 'any[]'
245
-
246
-
let z:any; // explicitly 'any'.
247
-
```
248
-
249
-
With TypeScript 2.1, instead of just choosing `any`, TypeScript will infer types based on what you end up assigning later on.
250
-
251
-
This is only enabled if `--noImplicitAny` is set.
252
-
253
-
##### Example
254
-
255
-
```ts
256
-
let x;
257
-
258
-
// You can still assign anything you want to 'x'.
259
-
x= () =>42;
260
-
261
-
// After that last assignment, TypeScript 2.1 knows that 'x' has type '() => number'.
262
-
let y =x();
263
-
264
-
// Thanks to that, it will now tell you that you can't add a number to a function!
265
-
console.log(x+y);
266
-
// ~~~~~
267
-
// Error! Operator '+' cannot be applied to types '() => number' and 'number'.
268
-
269
-
// TypeScript still allows you to assign anything you want to 'x'.
270
-
x="Hello world!";
271
-
272
-
// But now it also knows that 'x' is a 'string'!
273
-
x.toLowerCase();
274
-
```
275
-
276
-
The same sort of tracking is now also done for empty arrays.
277
-
278
-
A variable declared with no type annotation and an initial value of `[]` is considered an implicit `any[]` variable.
279
-
Each following `x.push(value)`, `x.unshift(value)` or `x[n] = value` operation _evolves_ the type of the variable in accordance with what elements are added to it.
280
-
281
-
```ts
282
-
function f1() {
283
-
let x = [];
284
-
x.push(5);
285
-
x[1] ="hello";
286
-
x.unshift(true);
287
-
returnx; // (string | number | boolean)[]
288
-
}
289
-
290
-
function f2() {
291
-
let x =null;
292
-
if (cond()) {
293
-
x= [];
294
-
while (cond()) {
295
-
x.push("hello");
296
-
}
297
-
}
298
-
returnx; // string[] | null
299
-
}
300
-
```
301
-
302
-
## Implicit any errors
303
-
304
-
One great benefit of this is that you'll see *way fewer* implicit `any` errors when running with `--noImplicitAny`.
305
-
Implicit `any` errors are only reported when the compiler is unable to know the type of a available without a type annotation.
306
-
307
-
##### Example
308
-
309
-
```ts
310
-
function f3() {
311
-
let x = []; // Error: Variable 'x' implicitly has type 'any[]' in some locations where its type cannot be determined.
312
-
x.push(5);
313
-
function g() {
314
-
x; // Error: Variable 'x' implicitly has an 'any[]' type.
315
-
}
316
-
}
317
-
```
318
-
319
238
# Downlevel Async Functions
320
239
321
240
This feature was supported before TypeScript 2.1, but only when targeting ES6/ES2015.
@@ -420,6 +339,87 @@ An import to a module with no declaration file will still be flagged as an error
420
339
import { x } from"asdf";
421
340
```
422
341
342
+
# Improved `any` Inference
343
+
344
+
Previously, if TypeScript can't figure out the type of a variable, it will choose the `any` type.
345
+
346
+
```ts
347
+
let x; // implicitly 'any'
348
+
let y = []; // implicitly 'any[]'
349
+
350
+
let z:any; // explicitly 'any'.
351
+
```
352
+
353
+
With TypeScript 2.1, instead of just choosing `any`, TypeScript will infer types based on what you end up assigning later on.
354
+
355
+
This is only enabled if `--noImplicitAny` is set.
356
+
357
+
##### Example
358
+
359
+
```ts
360
+
let x;
361
+
362
+
// You can still assign anything you want to 'x'.
363
+
x= () =>42;
364
+
365
+
// After that last assignment, TypeScript 2.1 knows that 'x' has type '() => number'.
366
+
let y =x();
367
+
368
+
// Thanks to that, it will now tell you that you can't add a number to a function!
369
+
console.log(x+y);
370
+
// ~~~~~
371
+
// Error! Operator '+' cannot be applied to types '() => number' and 'number'.
372
+
373
+
// TypeScript still allows you to assign anything you want to 'x'.
374
+
x="Hello world!";
375
+
376
+
// But now it also knows that 'x' is a 'string'!
377
+
x.toLowerCase();
378
+
```
379
+
380
+
The same sort of tracking is now also done for empty arrays.
381
+
382
+
A variable declared with no type annotation and an initial value of `[]` is considered an implicit `any[]` variable.
383
+
Each following `x.push(value)`, `x.unshift(value)` or `x[n] = value` operation _evolves_ the type of the variable in accordance with what elements are added to it.
384
+
385
+
```ts
386
+
function f1() {
387
+
let x = [];
388
+
x.push(5);
389
+
x[1] ="hello";
390
+
x.unshift(true);
391
+
returnx; // (string | number | boolean)[]
392
+
}
393
+
394
+
function f2() {
395
+
let x =null;
396
+
if (cond()) {
397
+
x= [];
398
+
while (cond()) {
399
+
x.push("hello");
400
+
}
401
+
}
402
+
returnx; // string[] | null
403
+
}
404
+
```
405
+
406
+
## Implicit any errors
407
+
408
+
One great benefit of this is that you'll see *way fewer* implicit `any` errors when running with `--noImplicitAny`.
409
+
Implicit `any` errors are only reported when the compiler is unable to know the type of a available without a type annotation.
410
+
411
+
##### Example
412
+
413
+
```ts
414
+
function f3() {
415
+
let x = []; // Error: Variable 'x' implicitly has type 'any[]' in some locations where its type cannot be determined.
416
+
x.push(5);
417
+
function g() {
418
+
x; // Error: Variable 'x' implicitly has an 'any[]' type.
419
+
}
420
+
}
421
+
```
422
+
423
423
# Better inference for literal types
424
424
425
425
String, numeric and boolean literal types (e.g. `"abc"`, `1`, and `true`) were previously inferred only in the presence of an explicit type annotation.
0 commit comments