Skip to content

Commit 343eb1c

Browse files
authored
Merge pull request #1039 from standard/ban-types
feat: @typescript-eslint/ban-types
2 parents 3a23a5d + 94fcc73 commit 343eb1c

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

src/index.test.ts

+48-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,54 @@ test('export', (t): void => {
9292
minimumDescriptionLength: 3
9393
}],
9494
'@typescript-eslint/ban-tslint-comment': 'error',
95+
'@typescript-eslint/ban-types': ['error', {
96+
extendDefaults: false,
97+
types: {
98+
String: {
99+
message: 'Use string instead',
100+
fixWith: 'string'
101+
},
102+
Boolean: {
103+
message: 'Use boolean instead',
104+
fixWith: 'boolean'
105+
},
106+
Number: {
107+
message: 'Use number instead',
108+
fixWith: 'number'
109+
},
110+
Symbol: {
111+
message: 'Use symbol instead',
112+
fixWith: 'symbol'
113+
},
114+
BigInt: {
115+
message: 'Use bigint instead',
116+
fixWith: 'bigint'
117+
},
118+
Function: {
119+
message: [
120+
'The `Function` type accepts any function-like value.',
121+
'It provides no type safety when calling the function, which can be a common source of bugs.',
122+
'It also accepts things like class declarations, which will throw at runtime as they will not be called with `new`.',
123+
'If you are expecting the function to accept certain arguments, you should explicitly define the function shape.'
124+
].join('\n')
125+
},
126+
// object typing
127+
Object: {
128+
message: [
129+
'The `Object` type actually means "any non-nullish value", so it is marginally better than `unknown`.',
130+
'- If you want a type meaning "any object", you probably want `Record<string, unknown>` instead.',
131+
'- If you want a type meaning "any value", you probably want `unknown` instead.'
132+
].join('\n')
133+
},
134+
'{}': {
135+
message: [
136+
'`{}` actually means "any non-nullish value".',
137+
'- If you want a type meaning "any object", you probably want `Record<string, unknown>` instead.',
138+
'- If you want a type meaning "any value", you probably want `unknown` instead.'
139+
].join('\n')
140+
}
141+
}
142+
}],
95143
'@typescript-eslint/brace-style': ['error', '1tbs', { allowSingleLine: true }],
96144
'@typescript-eslint/comma-dangle': ['error', {
97145
arrays: 'never',
@@ -371,7 +419,6 @@ test('all plugin rules are considered', (t) => {
371419
// This serves as a todo list and should ideally eventually end up empty
372420
// and then fail upon plugin upgrades where new rules are released.
373421
const notYetConsideredRules: string[] = [
374-
'ban-types',
375422
'class-literal-property-style',
376423
'consistent-generic-constructors',
377424
'consistent-type-exports',

src/index.ts

+48
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,54 @@ const config: Linter.Config = {
8181
minimumDescriptionLength: 3
8282
}],
8383
'@typescript-eslint/ban-tslint-comment': 'error',
84+
'@typescript-eslint/ban-types': ['error', {
85+
extendDefaults: false,
86+
types: {
87+
String: {
88+
message: 'Use string instead',
89+
fixWith: 'string'
90+
},
91+
Boolean: {
92+
message: 'Use boolean instead',
93+
fixWith: 'boolean'
94+
},
95+
Number: {
96+
message: 'Use number instead',
97+
fixWith: 'number'
98+
},
99+
Symbol: {
100+
message: 'Use symbol instead',
101+
fixWith: 'symbol'
102+
},
103+
BigInt: {
104+
message: 'Use bigint instead',
105+
fixWith: 'bigint'
106+
},
107+
Function: {
108+
message: [
109+
'The `Function` type accepts any function-like value.',
110+
'It provides no type safety when calling the function, which can be a common source of bugs.',
111+
'It also accepts things like class declarations, which will throw at runtime as they will not be called with `new`.',
112+
'If you are expecting the function to accept certain arguments, you should explicitly define the function shape.'
113+
].join('\n')
114+
},
115+
// object typing
116+
Object: {
117+
message: [
118+
'The `Object` type actually means "any non-nullish value", so it is marginally better than `unknown`.',
119+
'- If you want a type meaning "any object", you probably want `Record<string, unknown>` instead.',
120+
'- If you want a type meaning "any value", you probably want `unknown` instead.'
121+
].join('\n')
122+
},
123+
'{}': {
124+
message: [
125+
'`{}` actually means "any non-nullish value".',
126+
'- If you want a type meaning "any object", you probably want `Record<string, unknown>` instead.',
127+
'- If you want a type meaning "any value", you probably want `unknown` instead.'
128+
].join('\n')
129+
}
130+
}
131+
}],
84132
'@typescript-eslint/comma-dangle': ['error', {
85133
arrays: 'never',
86134
objects: 'never',

0 commit comments

Comments
 (0)