Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
bradzacher merged 1 commit intov4fromv4-typedef-defaults
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletionspackages/eslint-plugin/docs/rules/typedef.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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:

Expand All@@ -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 arenotrequired
- Type annotations on arrow function parameters are required
- Type annotations on variables are required
- Options otherwise adhere to the defaults

### `arrayDestructuring`

Expand Down
12 changes: 8 additions & 4 deletionspackages/eslint-plugin/src/rules/typedef.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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]) {
Expand Down
101 changes: 101 additions & 0 deletionspackages/eslint-plugin/tests/rules/typedef.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -492,6 +492,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
arrowParameter: true,
},
],
},
{
code: 'const receivesStrings = (a, b): void => {};',
Expand All@@ -505,6 +510,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
arrowParameter: true,
},
],
},
// Member variable declarations
{
Expand All@@ -519,6 +529,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
memberVariableDeclaration: true,
},
],
},
{
code: `
Expand All@@ -531,6 +546,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
memberVariableDeclaration: true,
},
],
},
// Function parameters
{
Expand All@@ -541,6 +561,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
parameter: true,
},
],
},
{
code: 'function receivesStrings(a, b): void {}',
Expand All@@ -554,6 +579,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
parameter: true,
},
],
},
{
code: 'function receivesNumber([a]): void {}',
Expand All@@ -563,6 +593,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
{
code: 'function receivesNumbers([a, b]): void {}',
Expand All@@ -572,6 +607,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
{
code: 'function receivesString({ a }): void {}',
Expand All@@ -581,6 +621,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
{
code: 'function receivesStrings({ a, b }): void {}',
Expand All@@ -590,6 +635,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
// Constructor parameters
{
Expand All@@ -604,6 +654,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
parameter: true,
},
],
},
{
code: `
Expand All@@ -617,6 +672,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
{
code: `
Expand All@@ -630,6 +690,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
// Method parameters
{
Expand All@@ -646,6 +711,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
parameter: true,
},
],
},
{
code: `
Expand All@@ -661,6 +731,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
{
code: `
Expand All@@ -674,6 +749,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
// Property declarations
{
Expand All@@ -688,6 +768,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
propertyDeclaration: true,
},
],
},
{
code: `
Expand All@@ -701,6 +786,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
propertyDeclaration: true,
},
],
},
{
code: `
Expand All@@ -714,6 +804,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
propertyDeclaration: true,
},
],
},
{
code: `
Expand All@@ -727,6 +822,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
propertyDeclaration: true,
},
],
},
// Variable declarations
{
Expand DownExpand Up@@ -879,6 +979,7 @@ class Foo {
],
options: [
{
memberVariableDeclaration: true,
variableDeclaration: true,
variableDeclarationIgnoreFunction: false,
},
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp