typedef
Require type annotations in certain places.
TypeScript 无法始终推断代码中所有位置的类型。 有些位置需要类型注释才能推断其类型。
英:TypeScript cannot always infer types for all places in code. Some locations require type annotations for their types to be inferred.
此规则可以在位置强制执行类型注释,无论它们是否需要。 这通常用于维护有时需要它们的元素类型的一致性。
英: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.
class ContainsText {
// There must be a type annotation here to infer the type
delayedText: string;
// `typedef` requires a type annotation here to maintain consistency
immediateTextExplicit: string = 'text';
// This is still a string type because of its initial value
immediateTextImplicit = 'text';
}
要强制执行调用签名上现有的类型定义,请使用
explicit-function-return-type
或explicit-module-boundary-types
。
不必要地要求类型注释可能会导致维护起来很麻烦,并且通常会降低代码的可读性。 TypeScript 通常比易于编写的类型注释更擅长推断类型。
英: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.
通常建议仅在有用时使用 --noImplicitAny
和 --strictPropertyInitialization
编译器选项来强制执行类型注释,而不是启用 typedef
。
英:Instead of enabling typedef
, it is generally recommended to use the --noImplicitAny
and --strictPropertyInitialization
compiler options to enforce type annotations only when useful.
module.exports = {
"rules": {
"@typescript-eslint/typedef": "error"
}
};
选项
例如,使用以下配置:
英:For example, with the following configuration:
{
"rules": {
"@typescript-eslint/typedef": [
"error",
{
"arrowParameter": true,
"variableDeclaration": true
}
]
}
}
- 箭头函数参数需要类型注释
- 需要对变量进行类型注释
arrayDestructuring
是否对使用数组解构声明的变量强制执行类型注释。
英:Whether to enforce type annotations on variables declared using array destructuring.
{ "arrayDestructuring": true }
的代码示例:
英:Examples of code with { "arrayDestructuring": true }
:
- ❌ 不正确
- ✅ 正确
const [a] = [1];
const [b, c] = [1, 2];
Open in Playgroundconst [a]: number[] = [1];
const [b]: [number] = [2];
const [c, d]: [boolean, string] = [true, 'text'];
for (const [key, val] of new Map([['key', 1]])) {
}
Open in PlaygroundarrowParameter
是否对箭头函数的参数强制执行类型注释。
英:Whether to enforce type annotations for parameters of arrow functions.
{ "arrowParameter": true }
的代码示例:
英:Examples of code with { "arrowParameter": true }
:
- ❌ 不正确
- ✅ 正确
const logsSize = size => console.log(size);
['hello', 'world'].map(text => text.length);
const mapper = {
map: text => text + '...',
};
Open in Playgroundconst logsSize = (size: number) => console.log(size);
['hello', 'world'].map((text: string) => text.length);
const mapper = {
map: (text: string) => text + '...',
};
Open in PlaygroundmemberVariableDeclaration
是否对类的成员变量强制执行类型注释。
英:Whether to enforce type annotations on member variables of classes.
{ "memberVariableDeclaration": true }
的代码示例:
英:Examples of code with { "memberVariableDeclaration": true }
:
- ❌ 不正确
- ✅ 正确
class ContainsText {
delayedText;
immediateTextImplicit = 'text';
}
Open in Playgroundclass ContainsText {
delayedText: string;
immediateTextImplicit: string = 'text';
}
Open in PlaygroundobjectDestructuring
是否对使用对象解构声明的变量强制执行类型注释。
英:Whether to enforce type annotations on variables declared using object destructuring.
{ "objectDestructuring": true }
的代码示例:
英:Examples of code with { "objectDestructuring": true }
:
- ❌ 不正确
- ✅ 正确
const { length } = 'text';
const [b, c] = Math.random() ? [1, 2] : [3, 4];
Open in Playgroundconst { length }: { length: number } = 'text';
const [b, c]: [number, number] = Math.random() ? [1, 2] : [3, 4];
for (const { key, val } of [{ key: 'key', val: 1 }]) {
}
Open in Playgroundparameter
是否对函数和方法的参数强制执行类型注释。
英:Whether to enforce type annotations for parameters of functions and methods.
{ "parameter": true }
的代码示例:
英:Examples of code with { "parameter": true }
:
- ❌ 不正确
- ✅ 正确
function logsSize(size): void {
console.log(size);
}
const doublesSize = function (size): number {
return size * 2;
};
const divider = {
curriesSize(size): number {
return size;
},
dividesSize: function (size): number {
return size / 2;
},
};
class Logger {
log(text): boolean {
console.log('>', text);
return true;
}
}
Open in Playgroundfunction logsSize(size: number): void {
console.log(size);
}
const doublesSize = function (size: number): number {
return size * 2;
};
const divider = {
curriesSize(size: number): number {
return size;
},
dividesSize: function (size: number): number {
return size / 2;
},
};
class Logger {
log(text: boolean): boolean {
console.log('>', text);
return true;
}
}
Open in PlaygroundpropertyDeclaration
是否对接口和类型的属性强制执行类型注释。
英:Whether to enforce type annotations for properties of interfaces and types.
{ "propertyDeclaration": true }
的代码示例:
英:Examples of code with { "propertyDeclaration": true }
:
- ❌ 不正确
- ✅ 正确
type Members = {
member;
otherMember;
};
Open in Playgroundtype Members = {
member: boolean;
otherMember: string;
};
Open in PlaygroundvariableDeclaration
是否对变量声明强制执行类型注释,不包括数组和对象解构。
英:Whether to enforce type annotations for variable declarations, excluding array and object destructuring.
{ "variableDeclaration": true }
的代码示例:
英:Examples of code with { "variableDeclaration": true }
:
- ❌ 不正确
- ✅ 正确
const text = 'text';
let initialText = 'text';
let delayedText;
Open in Playgroundconst text: string = 'text';
let initialText: string = 'text';
let delayedText: string;
Open in PlaygroundvariableDeclarationIgnoreFunction
忽略非箭头和箭头函数的变量声明。
英:Ignore variable declarations for non-arrow and arrow functions.
{ "variableDeclaration": true, "variableDeclarationIgnoreFunction": true }
的代码示例:
英:Examples of code with { "variableDeclaration": true, "variableDeclarationIgnoreFunction": true }
:
- ❌ 不正确
- ✅ 正确
const text = 'text';
Open in Playgroundconst a = (): void => {};
const b = function (): void => {};
const c: () => void = (): void => {};
class Foo {
a = (): void => {};
b = function (): void => {};
c = () => void = (): void => {};
}
Open in Playground何时不使用它
如果你使用更严格的 TypeScript 编译器选项,特别是 --noImplicitAny
和/或 --strictPropertyInitialization
,你可能不需要此规则。
英:If you are using stricter TypeScript compiler options, particularly --noImplicitAny
and/or --strictPropertyInitialization
, you likely don't need this rule.
一般来说,如果你不认为编写不必要的类型注释的成本合理,那么就不要使用此规则。
英:In general, if you do not consider the cost of writing unnecessary type annotations reasonable, then do not use this rule.
进一步阅读
选项
该规则接受以下选项
type Options = [
{
arrayDestructuring?: boolean;
arrowParameter?: boolean;
memberVariableDeclaration?: boolean;
objectDestructuring?: boolean;
parameter?: boolean;
propertyDeclaration?: boolean;
variableDeclaration?: boolean;
variableDeclarationIgnoreFunction?: boolean;
},
];
const defaultOptions: Options = [
{
arrayDestructuring: false,
arrowParameter: false,
memberVariableDeclaration: false,
objectDestructuring: false,
parameter: false,
propertyDeclaration: false,
variableDeclaration: false,
variableDeclarationIgnoreFunction: false,
},
];