Skip to content

Commit

Permalink
Merge pull request #1039 from standard/ban-types
Browse files Browse the repository at this point in the history
feat: @typescript-eslint/ban-types
  • Loading branch information
mightyiam authored Jan 17, 2023
2 parents 3a23a5d + 94fcc73 commit 343eb1c
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,54 @@ test('export', (t): void => {
minimumDescriptionLength: 3
}],
'@typescript-eslint/ban-tslint-comment': 'error',
'@typescript-eslint/ban-types': ['error', {
extendDefaults: false,
types: {
String: {
message: 'Use string instead',
fixWith: 'string'
},
Boolean: {
message: 'Use boolean instead',
fixWith: 'boolean'
},
Number: {
message: 'Use number instead',
fixWith: 'number'
},
Symbol: {
message: 'Use symbol instead',
fixWith: 'symbol'
},
BigInt: {
message: 'Use bigint instead',
fixWith: 'bigint'
},
Function: {
message: [
'The `Function` type accepts any function-like value.',
'It provides no type safety when calling the function, which can be a common source of bugs.',
'It also accepts things like class declarations, which will throw at runtime as they will not be called with `new`.',
'If you are expecting the function to accept certain arguments, you should explicitly define the function shape.'
].join('\n')
},
// object typing
Object: {
message: [
'The `Object` type actually means "any non-nullish value", so it is marginally better than `unknown`.',
'- If you want a type meaning "any object", you probably want `Record<string, unknown>` instead.',
'- If you want a type meaning "any value", you probably want `unknown` instead.'
].join('\n')
},
'{}': {
message: [
'`{}` actually means "any non-nullish value".',
'- If you want a type meaning "any object", you probably want `Record<string, unknown>` instead.',
'- If you want a type meaning "any value", you probably want `unknown` instead.'
].join('\n')
}
}
}],
'@typescript-eslint/brace-style': ['error', '1tbs', { allowSingleLine: true }],
'@typescript-eslint/comma-dangle': ['error', {
arrays: 'never',
Expand Down Expand Up @@ -371,7 +419,6 @@ test('all plugin rules are considered', (t) => {
// This serves as a todo list and should ideally eventually end up empty
// and then fail upon plugin upgrades where new rules are released.
const notYetConsideredRules: string[] = [
'ban-types',
'class-literal-property-style',
'consistent-generic-constructors',
'consistent-type-exports',
Expand Down
48 changes: 48 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,54 @@ const config: Linter.Config = {
minimumDescriptionLength: 3
}],
'@typescript-eslint/ban-tslint-comment': 'error',
'@typescript-eslint/ban-types': ['error', {
extendDefaults: false,
types: {
String: {
message: 'Use string instead',
fixWith: 'string'
},
Boolean: {
message: 'Use boolean instead',
fixWith: 'boolean'
},
Number: {
message: 'Use number instead',
fixWith: 'number'
},
Symbol: {
message: 'Use symbol instead',
fixWith: 'symbol'
},
BigInt: {
message: 'Use bigint instead',
fixWith: 'bigint'
},
Function: {
message: [
'The `Function` type accepts any function-like value.',
'It provides no type safety when calling the function, which can be a common source of bugs.',
'It also accepts things like class declarations, which will throw at runtime as they will not be called with `new`.',
'If you are expecting the function to accept certain arguments, you should explicitly define the function shape.'
].join('\n')
},
// object typing
Object: {
message: [
'The `Object` type actually means "any non-nullish value", so it is marginally better than `unknown`.',
'- If you want a type meaning "any object", you probably want `Record<string, unknown>` instead.',
'- If you want a type meaning "any value", you probably want `unknown` instead.'
].join('\n')
},
'{}': {
message: [
'`{}` actually means "any non-nullish value".',
'- If you want a type meaning "any object", you probably want `Record<string, unknown>` instead.',
'- If you want a type meaning "any value", you probably want `unknown` instead.'
].join('\n')
}
}
}],
'@typescript-eslint/comma-dangle': ['error', {
arrays: 'never',
objects: 'never',
Expand Down

0 comments on commit 343eb1c

Please sign in to comment.