forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
no-empty-object-type.ts
45 lines (43 loc) · 1.53 KB
/
no-empty-object-type.ts
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
import type { TSESLint } from '@typescript-eslint/utils';
import { createRule } from '../util';
export default createRule({
name: 'no-empty-object-type',
meta: {
type: 'suggestion',
docs: {
description: 'Disallow accidentally using the "empty object" type',
recommended: 'recommended',
},
hasSuggestions: true,
messages: {
banEmptyObjectType: [
'The `{}` ("empty object") type allows any non-nullish value, including literals like `0` and `""`.',
"- If that's what you want, disable this lint rule with an inline comment or in your ESLint config.",
'- If you want a type meaning "any object", you probably want `object` instead.',
'- If you want a type meaning "any value", you probably want `unknown` instead.',
'- If you want a type meaning "empty object", you probably want `Record<string, never>` instead.',
].join('\n'),
replaceEmptyObjectType: 'Replace `{}` with `{{replacement}}`.',
},
schema: [],
},
defaultOptions: [],
create(context) {
return {
'TSTypeLiteral[members.length=0]'(node): void {
context.report({
messageId: 'banEmptyObjectType',
node,
suggest: ['object', 'unknown', 'Record<string, never>'].map(
replacement => ({
data: { replacement },
messageId: 'replaceEmptyObjectType',
fix: (fixer): TSESLint.RuleFix =>
fixer.replaceText(node, replacement),
}),
),
});
},
};
},
});