generated from ForbesLindesay/web-app-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
.typescript.eslintrc.js
104 lines (88 loc) · 4.16 KB
/
.typescript.eslintrc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// const { readdirSync, existsSync } = require("fs")
module.exports = {
extends: ['./.eslintrc.js'],
overrides: [
{
files: ['*.ts', '*.tsx'],
parserOptions: {
// see: https://github.com/typescript-eslint/typescript-eslint/issues/2094
project: './tsconfig.json',
},
rules: {
// Using `import type` helps faster tools for compiling TypeScript (e.g. babel) correctly ignore type imports
'@typescript-eslint/consistent-type-imports': [
'error',
{prefer: 'type-imports', disallowTypeAnnotations: false},
],
// Being explicit about accessibility of methods/properties makes it clearer what is public/private in classes
'@typescript-eslint/explicit-member-accessibility': [
'error',
{
accessibility: 'explicit',
overrides: {
accessors: 'explicit',
constructors: 'off',
methods: 'explicit',
properties: 'explicit',
parameterProperties: 'explicit',
},
},
],
// This prevents errors where types that don't have a sensible `toString` get passed in template literals
'@typescript-eslint/no-base-to-string': [
'error',
{ignoredTypeNames: ['Error', 'RegExp']},
],
// Code like "delete container[name.toUpperCase()];" is hard to reason about, and normally means you should
// replace an object with a Map or Set
'@typescript-eslint/no-dynamic-delete': ['error'],
// Promises must be handled appropriately or explicitly marked as ignored with the `void` operator
'@typescript-eslint/no-floating-promises': [
'error',
{
ignoreVoid: true,
ignoreIIFE: false,
},
],
'@typescript-eslint/no-misused-promises': [
'error',
{
// Promises are always truthy
checksConditionals: true,
// Places that expect a function that returns `void` will not wait for or check for errors in functions that return `Promise`
checksVoidReturn: true,
},
],
// Type assertions (i.e. the `!` not null assertion or `as X`) are always unsafe
// leaving unnecessary ones in the code base makes it riskier to refactor later
// N.B. this rule is prone to false positives, but they can usually be resolved by either:
// 1. Using `import assertExists from "ts-assert-exists"` if the value is nullable and we were casting it to a non-nullable
// 2. Using the correct generics/annotating return types/annotating variable types if we were declaring the initial type for a value
'@typescript-eslint/no-unnecessary-type-assertion': ['error'],
// '@typescript-eslint/no-non-null-assertion': ['error'],
// '@typescript-eslint/no-unnecessary-condition': ['error'],
// This rule would block `any` as a return type for a function. Enabling it would require a lot of work though
// '@typescript-eslint/no-unsafe-return': ['error'],
// `arr.includes(x)` is much easier to read than `arr.indexOf(x) !== -1`
'@typescript-eslint/prefer-includes': ['error'],
// Make private class properties readonly when possible
'@typescript-eslint/prefer-readonly': ['error'],
// Good: [1, 2, 3].reduce<number[]>((arr, num) => arr.concat(num * 2), []);
// Bad: [1, 2, 3].reduce((arr, num) => arr.concat(num * 2), [] as number[]);
// The type cast is unsafe, and while it may be fine for now, creates refactoring hazards in the future
'@typescript-eslint/prefer-reduce-type-parameter': ['error'],
// These new methods are much more readable than other approaches
'@typescript-eslint/prefer-string-starts-ends-with': ['error'],
// Refactoring is much safer if you **always** await promises, even when they are returned from a function
'no-return-await': 0,
'@typescript-eslint/return-await': ['error', 'always'],
},
},
{
files: ['.build/**'],
rules: {
'react-hooks/rules-of-hooks': 0,
},
},
],
};