Skip to content

Commit 61127c1

Browse files
committed
Added section on using paths config to override type-definitions for npm libraries
1 parent 5142fb1 commit 61127c1

File tree

3 files changed

+74
-48
lines changed

3 files changed

+74
-48
lines changed

README.md

+37-24
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ This gives you the power to prioritize our work and support the project contribu
6969
- [Living Style Guide](#living-style-guide) 🌟 __NEW__
7070
- [Common Npm Scripts](#common-npm-scripts)
7171
- [Recipes](#recipes)
72-
- [tsconfig.json](#tsconfigjson)
73-
- [Vendor Types Augmentation](#vendor-types-augmentation)
72+
- [Baseline tsconfig.json](#baseline-tsconfigjson)
7473
- [Default and Named Module Exports](#default-and-named-module-exports)
74+
- [Type Augmentation for npm libraries](#type-augmentation-for-npm-libraries)
75+
- [Override type-definitions for npm libraries](#override-type-definitions-for-npm-libraries)
7576
- [FAQ](#faq)
7677
- [Tutorials](#tutorials)
7778
- [Contributors](#contributors)
@@ -1304,7 +1305,7 @@ configure({ adapter: new Adapter() });
13041305
13051306
# Recipes
13061307
1307-
### tsconfig.json
1308+
### Baseline tsconfig.json
13081309
- Recommended baseline config carefully optimized for strict type-checking and optimal webpack workflow
13091310
- Install [`tslib`](https://www.npmjs.com/package/tslib) to cut on bundle size, by using external runtime helpers instead of adding them inline: `npm i tslib`
13101311
- Example "paths" setup for baseUrl relative imports with Webpack
@@ -1314,21 +1315,22 @@ configure({ adapter: new Adapter() });
13141315
"compilerOptions": {
13151316
"baseUrl": "./", // enables project relative paths config
13161317
"paths": { // define paths mappings
1317-
"@src/*": ["src/*"] // will enable -> import { ... } from '@src/components'
1318-
// in webpack you need to add -> resolve: { alias: { '@src': PATH_TO_SRC } }
1318+
"@src/*": ["src/*"] // will enable import aliases -> import { ... } from '@src/components'
1319+
// WARNING: Add this to your webpack config -> resolve: { alias: { '@src': PATH_TO_SRC } }
1320+
// "redux": ["typings/redux"], // use an alternative type-definitions instead of the included one
13191321
},
13201322
"outDir": "dist/", // target for compiled files
13211323
"allowSyntheticDefaultImports": true, // no errors with commonjs modules interop
1322-
"esModuleInterop": true,
1324+
"esModuleInterop": true, // enable to do "import React ..." instead of "import * as React ..."
13231325
"allowJs": true, // include js files
13241326
"checkJs": true, // typecheck js files
13251327
"declaration": false, // don't emit declarations
1326-
"emitDecoratorMetadata": true,
1327-
"experimentalDecorators": true,
1328+
"emitDecoratorMetadata": true, // only if using decorators
1329+
"experimentalDecorators": true, // only if using decorators
13281330
"forceConsistentCasingInFileNames": true,
1329-
"importHelpers": true, // importing helper functions from tslib
1330-
"noEmitHelpers": true, // disable emitting inline helper functions
1331-
"jsx": "react", // process JSX
1331+
"importHelpers": true, // importing transpilation helpers from tslib
1332+
"noEmitHelpers": true, // disable inline transpilation helpers in each file
1333+
"jsx": "react", // translate JSX
13321334
"lib": [
13331335
"dom",
13341336
"es2016",
@@ -1339,9 +1341,6 @@ configure({ adapter: new Adapter() });
13391341
"moduleResolution": "node",
13401342
"noEmitOnError": true,
13411343
"noFallthroughCasesInSwitch": true,
1342-
"noImplicitAny": true,
1343-
"noImplicitReturns": true,
1344-
"noImplicitThis": true,
13451344
"noUnusedLocals": true,
13461345
"strict": true,
13471346
"pretty": true,
@@ -1388,10 +1387,10 @@ import Select from '@src/components/select';
13881387
13891388
[⇧ back to top](#table-of-contents)
13901389
1391-
### Vendor Types Augmentation
1392-
> Strategies to fix issues coming from broken "vendor type declarations" files (*.d.ts)
1390+
### Type Augmentation for npm libraries
1391+
Strategies to fix issues coming from external type-definitions files (*.d.ts)
13931392
1394-
#### Augmenting library internal type declarations - using relative import resolution
1393+
#### Augmenting library internal definitions - using relative import resolution
13951394
```ts
13961395
// added missing autoFocus Prop on Input component in "antd@2.10.0" npm package
13971396
declare module '../node_modules/antd/lib/input/Input' {
@@ -1401,9 +1400,7 @@ declare module '../node_modules/antd/lib/input/Input' {
14011400
}
14021401
```
14031402
1404-
[⇧ back to top](#table-of-contents)
1405-
1406-
#### Augmenting library public type declarations - using node module import resolution
1403+
#### Augmenting library public definitions - using node module import resolution
14071404
```ts
14081405
// fixed broken public type declaration in "rxjs@5.4.1" npm package
14091406
import { Operator } from 'rxjs/Operator';
@@ -1416,9 +1413,7 @@ declare module 'rxjs/Subject' {
14161413
}
14171414
```
14181415
1419-
[⇧ back to top](#table-of-contents)
1420-
1421-
#### To quick-fix missing type declarations for vendor modules you can "assert" a module type with `any` using [Shorthand Ambient Modules](https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Modules.md#shorthand-ambient-modules)
1416+
#### To quick-fix missing type-definitions for vendor modules you can "assert" a module type with `any` using [Shorthand Ambient Modules](https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Modules.md#shorthand-ambient-modules)
14221417
14231418
```tsx
14241419
// typings/modules.d.ts
@@ -1428,7 +1423,25 @@ declare module 'enzyme';
14281423

14291424
```
14301425
1431-
> More advanced scenarios for working with vendor module declarations can be found here [Official TypeScript Docs](https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Modules.md#working-with-other-javascript-libraries)
1426+
> More advanced scenarios for working with vendor type-definitions can be found here [Official TypeScript Docs](https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Modules.md#working-with-other-javascript-libraries)
1427+
1428+
[⇧ back to top](#table-of-contents)
1429+
1430+
### Override type-definitions for npm libraries
1431+
If you want to use an alternative (customized) type-definitions for some npm library (that usually comes with it's own type definitions), you can do it by adding an override in `paths` compiler option.
1432+
1433+
```ts
1434+
{
1435+
"compilerOptions": {
1436+
"baseUrl": ".",
1437+
"paths": {
1438+
"redux": ["typings/redux"], // use an alternative type-definitions instead of the included one
1439+
...
1440+
},
1441+
...,
1442+
}
1443+
}
1444+
```
14321445
14331446
[⇧ back to top](#table-of-contents)
14341447

docs/markdown/4_recipes.md

+34-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Recipes
22

3-
### tsconfig.json
3+
### Baseline tsconfig.json
44
- Recommended baseline config carefully optimized for strict type-checking and optimal webpack workflow
55
- Install [`tslib`](https://www.npmjs.com/package/tslib) to cut on bundle size, by using external runtime helpers instead of adding them inline: `npm i tslib`
66
- Example "paths" setup for baseUrl relative imports with Webpack
@@ -10,21 +10,22 @@
1010
"compilerOptions": {
1111
"baseUrl": "./", // enables project relative paths config
1212
"paths": { // define paths mappings
13-
"@src/*": ["src/*"] // will enable -> import { ... } from '@src/components'
14-
// in webpack you need to add -> resolve: { alias: { '@src': PATH_TO_SRC } }
13+
"@src/*": ["src/*"] // will enable import aliases -> import { ... } from '@src/components'
14+
// WARNING: Add this to your webpack config -> resolve: { alias: { '@src': PATH_TO_SRC } }
15+
// "redux": ["typings/redux"], // use an alternative type-definitions instead of the included one
1516
},
1617
"outDir": "dist/", // target for compiled files
1718
"allowSyntheticDefaultImports": true, // no errors with commonjs modules interop
18-
"esModuleInterop": true,
19+
"esModuleInterop": true, // enable to do "import React ..." instead of "import * as React ..."
1920
"allowJs": true, // include js files
2021
"checkJs": true, // typecheck js files
2122
"declaration": false, // don't emit declarations
22-
"emitDecoratorMetadata": true,
23-
"experimentalDecorators": true,
23+
"emitDecoratorMetadata": true, // only if using decorators
24+
"experimentalDecorators": true, // only if using decorators
2425
"forceConsistentCasingInFileNames": true,
25-
"importHelpers": true, // importing helper functions from tslib
26-
"noEmitHelpers": true, // disable emitting inline helper functions
27-
"jsx": "react", // process JSX
26+
"importHelpers": true, // importing transpilation helpers from tslib
27+
"noEmitHelpers": true, // disable inline transpilation helpers in each file
28+
"jsx": "react", // translate JSX
2829
"lib": [
2930
"dom",
3031
"es2016",
@@ -35,9 +36,6 @@
3536
"moduleResolution": "node",
3637
"noEmitOnError": true,
3738
"noFallthroughCasesInSwitch": true,
38-
"noImplicitAny": true,
39-
"noImplicitReturns": true,
40-
"noImplicitThis": true,
4139
"noUnusedLocals": true,
4240
"strict": true,
4341
"pretty": true,
@@ -84,10 +82,10 @@ import Select from '@src/components/select';
8482
8583
[⇧ back to top](#table-of-contents)
8684
87-
### Vendor Types Augmentation
88-
> Strategies to fix issues coming from broken "vendor type declarations" files (*.d.ts)
85+
### Type Augmentation for npm libraries
86+
Strategies to fix issues coming from external type-definitions files (*.d.ts)
8987
90-
#### Augmenting library internal type declarations - using relative import resolution
88+
#### Augmenting library internal definitions - using relative import resolution
9189
```ts
9290
// added missing autoFocus Prop on Input component in "antd@2.10.0" npm package
9391
declare module '../node_modules/antd/lib/input/Input' {
@@ -97,9 +95,7 @@ declare module '../node_modules/antd/lib/input/Input' {
9795
}
9896
```
9997
100-
[⇧ back to top](#table-of-contents)
101-
102-
#### Augmenting library public type declarations - using node module import resolution
98+
#### Augmenting library public definitions - using node module import resolution
10399
```ts
104100
// fixed broken public type declaration in "rxjs@5.4.1" npm package
105101
import { Operator } from 'rxjs/Operator';
@@ -112,12 +108,28 @@ declare module 'rxjs/Subject' {
112108
}
113109
```
114110
115-
[⇧ back to top](#table-of-contents)
116-
117-
#### To quick-fix missing type declarations for vendor modules you can "assert" a module type with `any` using [Shorthand Ambient Modules](https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Modules.md#shorthand-ambient-modules)
111+
#### To quick-fix missing type-definitions for vendor modules you can "assert" a module type with `any` using [Shorthand Ambient Modules](https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Modules.md#shorthand-ambient-modules)
118112
119113
::example='../../playground/typings/modules.d.ts'::
120114
121-
> More advanced scenarios for working with vendor module declarations can be found here [Official TypeScript Docs](https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Modules.md#working-with-other-javascript-libraries)
115+
> More advanced scenarios for working with vendor type-definitions can be found here [Official TypeScript Docs](https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Modules.md#working-with-other-javascript-libraries)
116+
117+
[⇧ back to top](#table-of-contents)
118+
119+
### Override type-definitions for npm libraries
120+
If you want to use an alternative (customized) type-definitions for some npm library (that usually comes with it's own type definitions), you can do it by adding an override in `paths` compiler option.
121+
122+
```ts
123+
{
124+
"compilerOptions": {
125+
"baseUrl": ".",
126+
"paths": {
127+
"redux": ["typings/redux"], // use an alternative type-definitions instead of the included one
128+
...
129+
},
130+
...,
131+
}
132+
}
133+
```
122134
123135
[⇧ back to top](#table-of-contents)

docs/markdown/_toc.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
- [Living Style Guide](#living-style-guide) 🌟 __NEW__
2626
- [Common Npm Scripts](#common-npm-scripts)
2727
- [Recipes](#recipes)
28-
- [tsconfig.json](#tsconfigjson)
29-
- [Vendor Types Augmentation](#vendor-types-augmentation)
28+
- [Baseline tsconfig.json](#baseline-tsconfigjson)
3029
- [Default and Named Module Exports](#default-and-named-module-exports)
30+
- [Type Augmentation for npm libraries](#type-augmentation-for-npm-libraries)
31+
- [Override type-definitions for npm libraries](#override-type-definitions-for-npm-libraries)
3132
- [FAQ](#faq)
3233
- [Tutorials](#tutorials)
3334
- [Contributors](#contributors)

0 commit comments

Comments
 (0)