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
However, when `isolatedModules` is enabled, the `AlbumFormat` enum will not be erased and TypeScript will raise an error.
185
177
186
178
This is because only `tsc` has enough context to understand what value `AlbumFormat.Vinyl` should have. TypeScript checks your entire project at once, and stores the values for `AlbumFormat` in memory.
By setting `noUncheckedIndexedAccess` to `true`, TypeScript will infer the type of every indexed access to be `T | undefined` instead of just `T`. In this case, every entry in `egoMirror.tracks` would be of type `string | undefined`:
258
250
259
-
```tsx
251
+
```ts twoslash
252
+
// @noUncheckedIndexedAccess: true
253
+
interfaceVinylSingle {
254
+
title:string;
255
+
artist:string;
256
+
tracks:string[];
257
+
}
258
+
259
+
const egoMirror:VinylSingle= {
260
+
title: "Ego / Mirror",
261
+
artist: "Burial / Four Tet / Thom Yorke",
262
+
tracks: ["Ego", "Mirror"],
263
+
};
264
+
265
+
// ---cut---
266
+
260
267
const ego =egoMirror.tracks[0];
268
+
// ^?
261
269
const mirror =egoMirror.tracks[1];
262
270
const nonExistentTrack =egoMirror.tracks[3];
263
-
264
-
// hovering over ego shows:
265
-
// const ego: string | undefined
266
271
```
267
272
268
273
However, because the types of each of the tracks are now `string | undefined`, we have errors when attempting to call `toUpperCase` even for the valid tracks:
269
274
270
-
```typescript
271
-
console.log(ego.toUpperCase()); // red squiggly line under ego
275
+
```ts twoslash
276
+
// @errors: 18048
277
+
// @noUncheckedIndexedAccess: true
278
+
interfaceVinylSingle {
279
+
title:string;
280
+
artist:string;
281
+
tracks:string[];
282
+
}
283
+
284
+
const egoMirror:VinylSingle= {
285
+
title: "Ego / Mirror",
286
+
artist: "Burial / Four Tet / Thom Yorke",
287
+
tracks: ["Ego", "Mirror"],
288
+
};
272
289
273
-
// hovering over ego shows
274
-
'ego'ispossibly'undefined'
290
+
const ego =egoMirror.tracks[0];
291
+
// ---cut---
292
+
console.log(ego.toUpperCase()); // red squiggly line under ego
275
293
```
276
294
277
295
This means that we have to handle the possibility of `undefined` values when accessing array or object indices.
@@ -524,21 +542,22 @@ If we keep `album.ts` the same, TypeScript will look up the directories for the
524
542
525
543
For example, consider this file `hello.cts` that uses the `export default` syntax:
526
544
527
-
```tsx
545
+
```ts twoslash
546
+
// @errors: 1286
547
+
// @verbatimModuleSyntax: true
548
+
// @module: NodeNext
549
+
// @moduleResolution: NodeNext
550
+
// @filename hello.cts
551
+
528
552
// hello.cts
529
553
const hello = () => {
530
554
console.log("Hello!");
531
555
};
532
556
533
-
export { hello };// red squiggly line under export { hello }
557
+
export { hello };
534
558
```
535
559
536
-
When `verbatimModuleSyntax` is enabled, TypeScript will show an error under the `export default` line that tells us we're mixing the syntaxes together:
When `verbatimModuleSyntax` is enabled, TypeScript will show an error under the `export default` line that tells us we're mixing the syntaxes together.
542
561
543
562
In order to fix the issue, we need to use the `export =` syntax instead:
544
563
@@ -555,11 +574,14 @@ This will compile down to `module.exports = { hello }` in the emitted JavaScript
555
574
556
575
The warnings will show when trying to use an ESM import as well:
557
576
558
-
```tsx
559
-
import { z } from"zod"; // rsl under import statement
577
+
```ts twoslash
578
+
// @errors: 1286
579
+
// @verbatimModuleSyntax: true
580
+
// @module: NodeNext
581
+
// @moduleResolution: NodeNext
582
+
// @filename hello.cts
560
583
561
-
// hovering over the import shows:
562
-
// ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
584
+
import { z } from"zod";
563
585
```
564
586
565
587
Here, the fix is to use `require` instead of `import`:
Trying to use `ONLY_ON_SERVER` in a file that's part of the `client` configuration will result in an error:
824
846
825
-
```tsx
847
+
```ts twoslash
848
+
// @errors: 2304
826
849
// inside client/index.ts
827
-
console.log(ONLY_ON_SERVER);// red squiggly line under ONLY_ON_SERVER
850
+
console.log(ONLY_ON_SERVER);
828
851
```
829
852
830
853
This feature is useful when dealing with environment-specific variables or globals that come from testing tools like Jest or Cypress, and avoids polluting the global scope.
0 commit comments