Skip to content
Permalink
Browse files

feat(eslint-plugin): [typedef] remove all defaults (#2352)

  • Loading branch information
bradzacher committed Aug 29, 2020
1 parent a3f163a commit a9cd6fb893074e4f2ca9ad3497eaddfacb3cfd25
@@ -16,29 +16,43 @@ class ContainsText {
}
```

> Note: requiring type annotations unnecessarily can be cumbersome to maintain and generally reduces code readability.
> TypeScript is often better at inferring types than easily written type annotations would allow.
> Instead of enabling `typedef`, it is generally recommended to use the `--noImplicitAny` and/or `--strictPropertyInitialization` compiler options to enforce type annotations only when useful.
**_Note:_** requiring type annotations unnecessarily can be cumbersome to maintain and generally reduces code readability.
TypeScript is often better at inferring types than easily written type annotations would allow.

**Instead of enabling `typedef`, it is generally recommended to use the `--noImplicitAny` and `--strictPropertyInitialization` compiler options to enforce type annotations only when useful.**

## Rule Details

This rule can enforce type annotations in locations regardless of whether they're required.
This is typically used to maintain consistency for element types that sometimes require them.

> To enforce type definitions existing on call signatures as per TSLint's `arrow-call-signature` and `call-signature` options, use `explicit-function-return-type`.
> To enforce type definitions existing on call signatures as per TSLint's `arrow-call-signature` and `call-signature` options, use `explicit-function-return-type`, or `explicit-module-boundary-types`.
## Options

This rule has an object option that may receive any of the following as booleans:
```ts
type Options = {
arrayDestructuring?: boolean;
arrowParameter?: boolean;
memberVariableDeclaration?: boolean;
objectDestructuring?: boolean;
parameter?: boolean;
propertyDeclaration?: boolean;
variableDeclaration?: boolean;
variableDeclarationIgnoreFunction?: boolean;
};
- `"arrayDestructuring"`
- `"arrowParameter"`: `true` by default
- `"memberVariableDeclaration"`: `true` by default
- `"objectDestructuring"`
- `"parameter"`: `true` by default
- `"propertyDeclaration"`: `true` by default
- `"variableDeclaration"`,
- `"variableDeclarationIgnoreFunction"`
const defaultOptions: Options = {
arrayDestructuring: false,
arrowParameter: false,
memberVariableDeclaration: false,
objectDestructuring: false,
parameter: false,
propertyDeclaration: false,
variableDeclaration: false,
variableDeclarationIgnoreFunction: false,
};
```

For example, with the following configuration:

@@ -48,17 +62,16 @@ For example, with the following configuration:
"@typescript-eslint/typedef": [
"error",
{
"arrowParameter": false,
"arrowParameter": true,
"variableDeclaration": true
}
]
}
}
```

- Type annotations on arrow function parameters are not required
- Type annotations on arrow function parameters are required
- Type annotations on variables are required
- Options otherwise adhere to the defaults

### `arrayDestructuring`

@@ -50,10 +50,14 @@ export default util.createRule<[Options], MessageIds>({
},
defaultOptions: [
{
[OptionKeys.ArrowParameter]: true,
[OptionKeys.MemberVariableDeclaration]: true,
[OptionKeys.Parameter]: true,
[OptionKeys.PropertyDeclaration]: true,
[OptionKeys.ArrayDestructuring]: false,
[OptionKeys.ArrowParameter]: false,
[OptionKeys.MemberVariableDeclaration]: false,
[OptionKeys.ObjectDestructuring]: false,
[OptionKeys.Parameter]: false,
[OptionKeys.PropertyDeclaration]: false,
[OptionKeys.VariableDeclaration]: false,
[OptionKeys.VariableDeclarationIgnoreFunction]: false,
},
],
create(context, [options]) {
@@ -492,6 +492,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
arrowParameter: true,
},
],
},
{
code: 'const receivesStrings = (a, b): void => {};',
@@ -505,6 +510,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
arrowParameter: true,
},
],
},
// Member variable declarations
{
@@ -519,6 +529,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
memberVariableDeclaration: true,
},
],
},
{
code: `
@@ -531,6 +546,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
memberVariableDeclaration: true,
},
],
},
// Function parameters
{
@@ -541,6 +561,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
parameter: true,
},
],
},
{
code: 'function receivesStrings(a, b): void {}',
@@ -554,6 +579,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
parameter: true,
},
],
},
{
code: 'function receivesNumber([a]): void {}',
@@ -563,6 +593,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
{
code: 'function receivesNumbers([a, b]): void {}',
@@ -572,6 +607,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
{
code: 'function receivesString({ a }): void {}',
@@ -581,6 +621,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
{
code: 'function receivesStrings({ a, b }): void {}',
@@ -590,6 +635,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
// Constructor parameters
{
@@ -604,6 +654,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
parameter: true,
},
],
},
{
code: `
@@ -617,6 +672,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
{
code: `
@@ -630,6 +690,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
// Method parameters
{
@@ -646,6 +711,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
parameter: true,
},
],
},
{
code: `
@@ -661,6 +731,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
{
code: `
@@ -674,6 +749,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
// Property declarations
{
@@ -688,6 +768,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
propertyDeclaration: true,
},
],
},
{
code: `
@@ -701,6 +786,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
propertyDeclaration: true,
},
],
},
{
code: `
@@ -714,6 +804,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
propertyDeclaration: true,
},
],
},
{
code: `
@@ -727,6 +822,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
propertyDeclaration: true,
},
],
},
// Variable declarations
{
@@ -879,6 +979,7 @@ class Foo {
],
options: [
{
memberVariableDeclaration: true,
variableDeclaration: true,
variableDeclarationIgnoreFunction: false,
},

0 comments on commit a9cd6fb

Please sign in to comment.