typedef
要求在特定位置使用類型註記。
TypeScript 無法始終推斷出程式碼中所有位置的類型。某些位置需要類型註記才能推斷其類型。
此規則可以強制在位置使用類型註記,無論是否需要。這通常用於保持有時需要註記的元素類型的ㄧ致性。
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 在推斷類型方面通常比容易編寫的類型註記更出色。
建議使用 --noImplicitAny
和 --strictPropertyInitialization
編譯器選項,而不是啟用 typedef
,以便僅在需要時才強制使用類型註記。
module.exports = {
"rules": {
"@typescript-eslint/typedef": "error"
}
};
在 Playground 中試用此規則 ↗
選項
此規則接受下列選項
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,
},
];
例如,使用下列設定
{
"rules": {
"@typescript-eslint/typedef": [
"error",
{
"arrowParameter": true,
"variableDeclaration": true
}
]
}
}
- 需要在箭頭函式參數上使用類型註記
- 需要在變數上使用類型註記
arrayDestructuring
是否要對使用陣列解構宣告的變數強制使用類型註記。
使用 { "arrayDestructuring": true }
的程式碼範例
- ❌ 錯誤
- ✅ 正確
const [a] = [1];
const [b, c] = [1, 2];
在 Playground 中開啟const [a]: number[] = [1];
const [b]: [number] = [2];
const [c, d]: [boolean, string] = [true, 'text'];
for (const [key, val] of new Map([['key', 1]])) {
}
在 Playground 中開啟arrowParameter
是否要對箭頭函式的參數強制使用類型註記。
使用 { "arrowParameter": true }
的程式碼範例
- ❌ 錯誤
- ✅ 正確
const logsSize = size => console.log(size);
['hello', 'world'].map(text => text.length);
const mapper = {
map: text => text + '...',
};
在 Playground 中開啟const logsSize = (size: number) => console.log(size);
['hello', 'world'].map((text: string) => text.length);
const mapper = {
map: (text: string) => text + '...',
};
在 Playground 中開啟memberVariableDeclaration
是否要對類別的成員變數強制使用類型註記。
使用 { "memberVariableDeclaration": true }
的程式碼範例
- ❌ 錯誤
- ✅ 正確
class ContainsText {
delayedText;
immediateTextImplicit = 'text';
}
在 Playground 中開啟class ContainsText {
delayedText: string;
immediateTextImplicit: string = 'text';
}
在 Playground 中開啟objectDestructuring
是否要對使用物件解構宣告的變數強制使用類型註記。
使用 { "objectDestructuring": true }
的程式碼範例
- ❌ 錯誤
- ✅ 正確
const { length } = 'text';
const [b, c] = Math.random() ? [1, 2] : [3, 4];
在 Playground 中開啟const { length }: { length: number } = 'text';
const [b, c]: [number, number] = Math.random() ? [1, 2] : [3, 4];
for (const { key, val } of [{ key: 'key', val: 1 }]) {
}
在 Playground 中開啟parameter
是否要對函式和方法的參數強制使用類型註記。
使用 { "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;
}
}
在 Playground 中開啟function 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;
}
}
在 Playground 中開啟propertyDeclaration
是否要對介面和類型的屬性強制使用類型註記。
使用 { "propertyDeclaration": true }
的程式碼範例
- ❌ 錯誤
- ✅ 正確
type Members = {
member;
otherMember;
};
在 Playground 中開啟type Members = {
member: boolean;
otherMember: string;
};
在 Playground 中開啟variableDeclaration
是否要對變數宣告強制使用類型註記,但不包括陣列和物件解構。
使用 { "variableDeclaration": true }
的程式碼範例
- ❌ 錯誤
- ✅ 正確
const text = 'text';
let initialText = 'text';
let delayedText;
在 Playground 中開啟const text: string = 'text';
let initialText: string = 'text';
let delayedText: string;
在 Playground 中開啟variableDeclarationIgnoreFunction
忽略非箭頭函式和箭頭函式的變數宣告。
使用 { "variableDeclaration": true, "variableDeclarationIgnoreFunction": true }
的程式碼範例
- ❌ 錯誤
- ✅ 正確
const text = 'text';
在 Playground 中開啟const a = (): void => {};
const b = function (): void {};
const c: () => void = (): void => {};
class Foo {
a = (): void => {};
b = function (): void {};
c: () => void = (): void => {};
}
在 Playground 中開啟何時不應使用
如果您使用的是更嚴格的 TypeScript 編譯器選項,尤其是 --noImplicitAny
和/或 --strictPropertyInitialization
,則您可能不需要此規則。
一般來說,如果您認為編寫不必要的類型註記的成本不合理,則請勿使用此規則。