Skip to content

Commit 9fbbcaa

Browse files
committed
Add experimental TypeScript configuration
1 parent 711e90d commit 9fbbcaa

File tree

9 files changed

+4223
-199
lines changed

9 files changed

+4223
-199
lines changed

README.md

Lines changed: 37 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,41 @@ module.exports = {
2424
};
2525
```
2626

27+
### TypeScript
28+
29+
TypeScript support is currently experimental, and available separately. Make sure to install `typescript` v3 or v4 on your project, then:
30+
31+
```js
32+
module.exports = {
33+
// See https://github.com/torchbox/eslint-config-torchbox for rules.
34+
extends: 'torchbox/typescript',
35+
};
36+
```
37+
38+
The TypeScript configuration uses the same rules as the base configuration, with two exceptions:
39+
40+
- Rules which will be checked by the TypeScript compiler anyway are disabled.
41+
- Rules which would work differently for TypeScript code have been replaced by their [@typescript-eslint](https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/eslint-plugin), where this is possible without requiring type checking as part of linting (see [Design decisions](#design-decisions)).
42+
43+
#### Advanced TypeScript usage
44+
45+
For projects wanting stricter checks, consider using [linting with type information](https://typescript-eslint.io/docs/linting/type-linting/) Here is a sample ESLint configuration:
46+
47+
```js
48+
module.exports = {
49+
// See https://github.com/torchbox/eslint-config-torchbox for rules.
50+
extends: [
51+
'torchbox/typescript',
52+
'plugin:@typescript-eslint/recommended-requiring-type-checking',
53+
],
54+
// See https://typescript-eslint.io/docs/linting/type-linting/.
55+
parserOptions: {
56+
tsconfigRootDir: __dirname,
57+
project: ['./tsconfig.json'],
58+
},
59+
};
60+
```
61+
2762
### Tips
2863

2964
#### Common CLI flags
@@ -72,70 +107,6 @@ module.exports = {
72107
};
73108
```
74109

75-
### TypeScript
76-
77-
This config doesn’t include TypeScript support out of the box. We can install and configure a TypeScript parser and ESLint plugin to make it compatible. Here is how to proceed:
78-
79-
```sh
80-
npm install --save-dev @typescript-eslint/parser @typescript-eslint/eslint-plugin
81-
```
82-
83-
And update your ESLint configuration to:
84-
85-
```js
86-
module.exports = {
87-
// See https://github.com/torchbox/eslint-config-torchbox for rules.
88-
extends: 'torchbox',
89-
// Using Babel parser for experimental syntax
90-
parser: 'babel-eslint',
91-
overrides: [
92-
{
93-
files: ['*.ts', '*.tsx'],
94-
extends: [
95-
'torchbox',
96-
'plugin:import/typescript',
97-
'plugin:@typescript-eslint/eslint-recommended',
98-
'plugin:@typescript-eslint/recommended',
99-
],
100-
parser: '@typescript-eslint/parser',
101-
plugins: ['@typescript-eslint'],
102-
rules: {
103-
'react/jsx-filename-extension': [
104-
2,
105-
// Allow mixed JSX in JavaScript files.
106-
{ extensions: ['.js', '.tsx'] },
107-
],
108-
'import/extensions': [
109-
2,
110-
'always',
111-
{
112-
ignorePackages: true,
113-
pattern: {
114-
js: 'never',
115-
jsx: 'never',
116-
ts: 'never',
117-
tsx: 'never',
118-
},
119-
},
120-
],
121-
},
122-
},
123-
],
124-
};
125-
```
126-
127-
As of ESLint v6, you will also need to tell ESLint to parse TypeScript files with the `--ext` flag:
128-
129-
```sh
130-
eslint --report-unused-disable-directives --ext .js,.jsx,.ts,.tsx .
131-
```
132-
133-
Note that the TypeScript-friendly rules included in the config above aren’t as strict as our baseline config. To bridge this gap, consider using `--max-warnings 0` to treat all warnings as errors:
134-
135-
```sh
136-
eslint --max-warnings 0 --report-unused-disable-directives --ext .js,.jsx,.ts,.tsx .
137-
```
138-
139110
### React
140111

141112
This config is meant first and foremost for React projects, where it will detect which rules to apply based on the version of React used on the project. The config can also be used on non-React projects – just make sure to disable the version check by adding: the following in your config:
@@ -184,7 +155,8 @@ module.exports = {
184155

185156
- [`airbnb`](https://www.npmjs.com/package/eslint-config-airbnb)
186157
- [`airbnb/hooks`](https://www.npmjs.com/package/eslint-config-airbnb)
187-
- [`prettier`](https://github.com/prettier/eslint-config-prettier) (including configuration for TypeScript, React, Vue)
158+
- [`prettier`](https://github.com/prettier/eslint-config-prettier)
159+
- Additionally for TypeScript, [`plugin:@typescript-eslint/recommended`](https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/eslint-plugin#supported-rules)
188160

189161
<!-- Generated with: npm run build -->
190162

jest.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
// Customise Jest module resolution as it lacks compatibility for Node `exports`.
3+
// See:
4+
// - https://nodejs.org/api/packages.html#exports
5+
// - https://github.com/browserify/resolve/issues/222
6+
// - https://github.com/facebook/jest/issues/9565
7+
// - https://github.com/facebook/jest/issues/9430
8+
moduleNameMapper: {
9+
'^eslint/use-at-your-own-risk$': 'eslint/lib/unsupported-api.js',
10+
},
11+
};

0 commit comments

Comments
 (0)