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
If we try to import this file into a TypeScript file, we'll get an error:
147
147
148
-
```typescript
148
+
```ts twoslash
149
+
// @errors: 2307
149
150
// inside of app.ts
150
151
151
-
import { playTrack } from"./musicPlayer"; // red squiggly line under ./musicPlayer
152
-
// Hovering over the error shows:
153
-
// Could not find a declaration file for module './musicPlayer'.
152
+
import { playTrack } from"./musicPlayer";
154
153
```
155
154
156
155
This error occurs because TypeScript doesn't have any type information for the `musicPlayer.js` file. To fix this, we can create a declaration file with the same name as the JavaScript file, but with a `.d.ts` extension:
@@ -334,18 +333,22 @@ declare function myFunction(): void;
334
333
335
334
To do this, we can wrap our `declare const` statement in a `declare global` block:
336
335
337
-
```typescript
336
+
```ts twoslash
337
+
// @errors: 1038
338
+
typeAlbum= {
339
+
title:string;
340
+
artist:string;
341
+
releaseDate:string;
342
+
};
343
+
344
+
// ---cut---
338
345
// inside musicUtils.ts
339
346
declareglobal {
340
347
declareconst ALBUM_API: {
341
-
// red squiggly line under declare
342
348
getAlbumInfo(upc:string):Promise<Album>;
343
349
searchAlbums(query:string):Promise<Album[]>;
344
350
};
345
351
}
346
-
347
-
// Hovering over the error shows:
348
-
// A 'declare' modifier cannot be used in an already ambient context.
349
352
```
350
353
351
354
This almost works, except for the error. We can't use `declare` inside an ambient context: the `declare global` block is already ambient. So, we can remove the `declare` keyword:
@@ -704,14 +707,10 @@ In some environments like Webpack, it's possible to import files like images tha
704
707
705
708
Consider this example where several `.png` images are imported. TypeScript doesn't typically recognize PNG files as modules, so it reports an error underneath each import statement:
706
709
707
-
```tsx
708
-
importpngUrl1from"./example1.png"; // red squiggly line under "./example1.png"
709
-
importpngUrl2from"./example2.png"; // red squiggly line under "./example2.png"
710
-
importpngUrl3from"./example3.png"; // red squiggly line under "./example3.png"
711
-
importpngUrl4from"./example4.png"; // red squiggly line under "./example4.png"
const state =window.DEBUG.getState(); // red squiggly line under DEBUG
@@ -835,12 +837,7 @@ type test = Expect<Equal<typeof state, { id: string }>>;
835
837
836
838
We expect `state` to be an object with an `id` string property, but it is currently typed as `any`.
837
839
838
-
There's also an error on `DEBUG` that tells us TypeScript doesn't see the `DEBUG` type:
839
-
840
-
```tsx
841
-
// hovering over DEBUG shows:
842
-
// Property 'DEBUG' does not exist on type 'Window & typeof globalThis'.
843
-
```
840
+
There's also an error on `DEBUG` that tells us TypeScript doesn't see the `DEBUG` type.
844
841
845
842
Your task is to specify that `DEBUG` is available on the `window` object. This will help TypeScript understand the type of `state` and provide the expected type checking.
846
843
@@ -852,10 +849,18 @@ The `env` property is an object encapsulating all the environment variables that
852
849
853
850
Here's an example of using an `envVariable`, along with a test that checks to see if it is a string:
TypeScript isn't aware of the `MY_ENV_VAR` environment variable, so it can't be certain that it will be a string. Thus, the `Equal` test fails because `envVariable` is typed as `string | undefined` instead of just `string`.
@@ -889,8 +894,13 @@ declare const DEBUG: {};
889
894
890
895
Now that we've typed `DEBUG`, the error message has moved to be under `getState()`:
891
896
892
-
```tsx
893
-
const state =DEBUG.getState(); // red squiggly line under getState
0 commit comments