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
The second change is that rest elements can occur anywhere in a tuple - not just at the end!
93
93
94
-
```ts twoslash
94
+
```ts
95
95
typeStrings= [string, string];
96
96
typeNumbers= [number, number];
97
97
@@ -109,7 +109,7 @@ But with TypeScript 4.0, this restriction is relaxed.
109
109
110
110
Note that in cases when we spread in a type without a known length, the resulting type becomes unbounded as well, and all the following elements factor into the resulting rest element type.
111
111
112
-
```ts twoslash
112
+
```ts
113
113
typeStrings= [string, string];
114
114
typeNumbers=number[];
115
115
@@ -119,7 +119,7 @@ type Unbounded = [...Strings, ...Numbers, boolean];
119
119
120
120
By combining both of these behaviors together, we can write a single well-typed signature for `concat`:
121
121
122
-
```ts twoslash
122
+
```ts
123
123
typeArr=readonlyany[];
124
124
125
125
function concat<TextendsArr, UextendsArr>(arr1:T, arr2:U): [...T, ...U] {
@@ -142,7 +142,7 @@ function partialCall(f, ...headArgs) {
142
142
143
143
TypeScript 4.0 improves the inference process for rest parameters and rest tuple elements so that we can type this and have it "just work".
144
144
145
-
```ts twoslash
145
+
```ts
146
146
typeArr=readonlyunknown[];
147
147
148
148
function partialCall<TextendsArr, UextendsArr, R>(
@@ -155,7 +155,7 @@ function partialCall<T extends Arr, U extends Arr, R>(
155
155
156
156
In this case, `partialCall` understands which parameters it can and can't initially take, and returns functions that appropriately accept and reject anything left over.
For one, when labeling a tuple element, all other elements in the tuple must also be labeled.
246
246
247
-
```ts twoslash
247
+
```ts
248
248
// @errors: 5084
249
249
typeBar= [first: string, number];
250
250
```
251
251
252
252
It's worth noting - labels don't require us to name our variables differently when destructuring.
253
253
They're purely there for documentation and tooling.
254
254
255
-
```ts twoslash
255
+
```ts
256
256
function foo(x: [first: string, second: number]) {
257
257
// ...
258
258
@@ -277,7 +277,7 @@ To learn more, check out [the pull request](https://github.com/microsoft/TypeScr
277
277
TypeScript 4.0 can now use control flow analysis to determine the types of properties in classes when `noImplicitAny` is enabled.
278
278
279
279
<!--prettier-ignore -->
280
-
```ts twoslash
280
+
```ts
281
281
classSquare {
282
282
// Previously both of these were any
283
283
area;
@@ -294,7 +294,7 @@ class Square {
294
294
In cases where not all paths of a constructor assign to an instance member, the property is considered to potentially be `undefined`.
295
295
296
296
<!--prettier-ignore -->
297
-
```ts twoslash
297
+
```ts
298
298
// @errors: 2532
299
299
classSquare {
300
300
sideLength;
@@ -314,7 +314,7 @@ class Square {
314
314
315
315
In cases where you know better (e.g. you have an `initialize` method of some sort), you'll still need an explicit type annotation along with a definite assignment assertion (`!`) if you're in `strictPropertyInitialization`.
316
316
317
-
```ts twoslash
317
+
```ts
318
318
classSquare {
319
319
// definite assignment assertion
320
320
// v
@@ -421,7 +421,7 @@ if (!obj.prop) {
421
421
422
422
[Try running the following example](https://www.typescriptlang.org/play?ts=Nightly#code/MYewdgzgLgBCBGArGBeGBvAsAKBnmA5gKawAOATiKQBQCUGO+TMokIANkQHTsgHUAiYlChFyMABYBDCDHIBXMANoBuHI2Z4A9FpgAlIqXZTgRGAFsiAQg2byJeeTAwAslKgSu5KWAAmIczoYAB4YAAYuAFY1XHwAXwAaWxgIEhgKKmoAfQA3KXYALhh4EA4iH3osWM1WCDKePkFUkTFJGTlFZRimOJw4mJwAM0VgKABLcBhB0qCqplr63n4BcjGCCVgIMd8zIjz2eXciXy7k+yhHZygFIhje7BwFzgblgBUJMdlwM3yAdykAJ6yBSQGAeMzNUTkU7YBCILgZUioOBIBGUJEAHwxUxmqnU2Ce3CWgnenzgYDMACo6pZxpYIJSOqDwSkSFCYXC0VQYFi0NMQHQVEA) to see how that differs from _always_ performing the assignment.
423
423
424
-
```ts twoslash
424
+
```ts
425
425
const obj = {
426
426
get prop() {
427
427
console.log("getter has run");
@@ -456,7 +456,7 @@ You can also [check out TC39's proposal repository for this feature](https://git
456
456
Since the beginning days of TypeScript, `catch` clause variables have always been typed as `any`.
457
457
This meant that TypeScript allowed you to do anything you wanted with them.
458
458
459
-
```ts twoslash
459
+
```ts
460
460
try {
461
461
// Do some work
462
462
} catch (x) {
@@ -475,7 +475,7 @@ That's why TypeScript 4.0 now lets you specify the type of `catch` clause variab
475
475
`unknown` is safer than `any` because it reminds us that we need to perform some sorts of type-checks before operating on our values.
476
476
477
477
<!--prettier-ignore -->
478
-
```ts twoslash
478
+
```ts
479
479
// @errors: 2571
480
480
try {
481
481
// ...
@@ -520,7 +520,7 @@ As an example, the following `tsconfig.json` file tells TypeScript to transform
520
520
In cases where you need to have a different JSX factory on a per-file basis<!-- (maybe you like to ship React, Preact, and Inferno to give a blazing fast experience) -->, you can take advantage of the new `/** @jsxFrag */` pragma comment.
521
521
For example, the following...
522
522
523
-
```tsx twoslash
523
+
```tsx
524
524
// @noErrors
525
525
// Note: these pragma comments need to be written
526
526
// with a JSDoc-style multiline syntax to take effect.
@@ -539,7 +539,7 @@ export const Header = (
539
539
540
540
...will get transformed to this output JavaScript...
541
541
542
-
```tsx twoslash
542
+
```tsx
543
543
// @noErrors
544
544
// @showEmit
545
545
// Note: these pragma comments need to be written
@@ -682,7 +682,7 @@ MDN recommends moving to [`self.origin`](https://developer.mozilla.org/en-US/doc
682
682
683
683
Previously, it was only an error for properties to override accessors, or accessors to override properties, when using `useDefineForClassFields`; however, TypeScript now always issues an error when declaring a property in a derived class that would override a getter or setter in the base class.
684
684
685
-
```ts twoslash
685
+
```ts
686
686
// @errors: 1049 2610
687
687
classBase {
688
688
get foo() {
@@ -698,7 +698,7 @@ class Derived extends Base {
698
698
}
699
699
```
700
700
701
-
```ts twoslash
701
+
```ts
702
702
// @errors: 2611
703
703
classBase {
704
704
prop =10;
@@ -718,7 +718,7 @@ See more details on [the implementing pull request](https://github.com/microsoft
718
718
When using the `delete` operator in `strictNullChecks`, the operand must now be `any`, `unknown`, `never`, or be optional (in that it contains `undefined` in the type).
719
719
Otherwise, use of the `delete` operator is an error.
0 commit comments