跳至主要內容

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-typeexplicit-module-boundary-types

注意

不必要地要求類型註記可能會導致維護變得繁瑣,並且通常會降低程式碼的可讀性。TypeScript 在推斷類型方面通常比容易編寫的類型註記更出色。

建議使用 --noImplicitAny--strictPropertyInitialization 編譯器選項,而不是啟用 typedef,以便僅在需要時才強制使用類型註記。

.eslintrc.cjs
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 中開啟

arrowParameter

是否要對箭頭函式的參數強制使用類型註記。

使用 { "arrowParameter": true } 的程式碼範例

const logsSize = size => console.log(size);

['hello', 'world'].map(text => text.length);

const mapper = {
map: text => text + '...',
};
在 Playground 中開啟

memberVariableDeclaration

是否要對類別的成員變數強制使用類型註記。

使用 { "memberVariableDeclaration": true } 的程式碼範例

class ContainsText {
delayedText;
immediateTextImplicit = 'text';
}
在 Playground 中開啟

objectDestructuring

是否要對使用物件解構宣告的變數強制使用類型註記。

使用 { "objectDestructuring": true } 的程式碼範例

const { length } = 'text';
const [b, c] = Math.random() ? [1, 2] : [3, 4];
在 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 中開啟

propertyDeclaration

是否要對介面和類型的屬性強制使用類型註記。

使用 { "propertyDeclaration": true } 的程式碼範例

type Members = {
member;
otherMember;
};
在 Playground 中開啟

variableDeclaration

是否要對變數宣告強制使用類型註記,但不包括陣列和物件解構。

使用 { "variableDeclaration": true } 的程式碼範例

const text = 'text';
let initialText = 'text';
let delayedText;
在 Playground 中開啟

variableDeclarationIgnoreFunction

忽略非箭頭函式和箭頭函式的變數宣告。

使用 { "variableDeclaration": true, "variableDeclarationIgnoreFunction": true } 的程式碼範例

const text = 'text';
在 Playground 中開啟

何時不應使用

如果您使用的是更嚴格的 TypeScript 編譯器選項,尤其是 --noImplicitAny 和/或 --strictPropertyInitialization,則您可能不需要此規則。

一般來說,如果您認為編寫不必要的類型註記的成本不合理,則請勿使用此規則。

進一步閱讀

資源