Skip to content

Commit be282c1

Browse files
fix(typescript-guide): improve typescript guide
1 parent 58a7148 commit be282c1

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/content/guides/typescript.mdx

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Now we'll modify the directory structure & the configuration files:
3535
├── package.json
3636
├── package-lock.json
3737
+ ├── tsconfig.json
38-
- ├── webpack.config.ts
38+
- ├── webpack.config.js
3939
+ ├── webpack.config.ts
4040
├── /dist
4141
│ ├── bundle.js
@@ -55,12 +55,14 @@ Let's set up a configuration to support JSX and compile TypeScript down to ES5..
5555
"compilerOptions": {
5656
"outDir": "./dist/",
5757
"noImplicitAny": true,
58-
"module": "es6",
59-
"target": "es5",
60-
"jsx": "react",
61-
"allowJs": true,
62-
"moduleResolution": "node"
63-
}
58+
"module": "esnext",
59+
"moduleResolution": "bundler",
60+
"target": "esnext",
61+
"jsx": "react-jsx",
62+
"allowJs": true
63+
},
64+
"include": ["src/**/*"],
65+
"exclude": ["node_modules"]
6466
}
6567
```
6668

@@ -116,7 +118,14 @@ export default config;
116118
117119
This will direct webpack to _enter_ through `./index.ts`, _load_ all `.ts` and `.tsx` files through the `ts-loader`, and _output_ a `bundle.js` file in our current directory.
118120

119-
Now lets change the import of `lodash` in our `./index.ts` due to the fact that there is no default export present in `lodash` definitions.
121+
Next, we need to adjust how we import `lodash` in our `./index.ts`. Since the lodash definitions don't include a default export, we'll need to update our import statement.
122+
First, make sure to install the [TypeScript definitions](#using-third-party-libraries):
123+
124+
```bash
125+
npm install --save-dev @types/lodash
126+
```
127+
128+
Then, update your import at the top of the file:
120129

121130
**./index.ts**
122131

@@ -177,7 +186,7 @@ If you use [`compilerOptions.paths`](https://www.typescriptlang.org/tsconfig#pat
177186

178187
`resolve.tsconfig` accepts `boolean | string | object`:
179188

180-
**webpack.config.js**
189+
**webpack.config.ts**
181190

182191
```js
183192
export default {
@@ -251,18 +260,20 @@ To enable source maps, we must configure TypeScript to output inline source maps
251260
"outDir": "./dist/",
252261
+ "sourceMap": true,
253262
"noImplicitAny": true,
254-
"module": "commonjs",
255-
"target": "es5",
256-
"jsx": "react",
263+
"module": "esnext",
264+
"moduleResolution": "bundler",
265+
"target": "esnext",
266+
"jsx": "react-jsx",
257267
"allowJs": true,
258-
"moduleResolution": "node",
259-
}
268+
},
269+
"include": ["src/**/*"],
270+
"exclude": ["node_modules"]
260271
}
261272
```
262273

263274
Now we need to tell webpack to extract these source maps and include in our final bundle:
264275

265-
**webpack.config.js**
276+
**webpack.config.ts**
266277

267278
```diff
268279
import path from "node:path";
@@ -320,7 +331,7 @@ To enable the types for the whole project, add `webpack/module` to `compilerOpti
320331

321332
When installing third party libraries from npm, it is important to remember to install the typing definition for that library.
322333

323-
For example, if we want to install lodash we can run the following command to get the typings for it:
334+
For example, if we want to use lodash, we should run the following command to install its type definitions:
324335

325336
```bash
326337
npm install --save-dev @types/lodash

0 commit comments

Comments
 (0)