前往主要內容

明確模組邊界類型

在匯出的函式和類別公共類別方法上要求明確傳回和引數的型別。

函式傳回值和引數的明確型別,可讓任何呼叫碼清楚了解模組界限的輸入和輸出。加入這些型別的明確型別註解,有助於提升程式碼可讀性。這也可改善大型程式碼庫中 TypeScript 型別檢查效能。

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/explicit-module-boundary-types": "error"
}
};

在遊樂場↗嘗試這條規則

範例

// Should indicate that no value is returned (void)
export function test() {
return;
}

// Should indicate that a string is returned
export var arrowFn = () => 'test';

// All arguments should be typed
export var arrowFn = (arg): string => `test ${arg}`;
export var arrowFn = (arg: any): string => `test ${arg}`;

export class Test {
// Should indicate that no value is returned (void)
method() {
return;
}
}
在遊樂場中開啟

選項

這條規則接受下列選項

type Options = [
{
/** Whether to ignore arguments that are explicitly typed as `any`. */
allowArgumentsExplicitlyTypedAsAny?: boolean;
/**
* Whether to ignore return type annotations on body-less arrow functions that return an `as const` type assertion.
* You must still type the parameters of the function.
*/
allowDirectConstAssertionInArrowFunctions?: boolean;
/**
* Whether to ignore return type annotations on functions immediately returning another function expression.
* You must still type the parameters of the function.
*/
allowHigherOrderFunctions?: boolean;
/** Whether to ignore type annotations on the variable of a function expression. */
allowTypedFunctionExpressions?: boolean;
/** An array of function/method names that will not have their arguments or return values checked. */
allowedNames?: string[];
},
];

const defaultOptions: Options = [
{
allowArgumentsExplicitlyTypedAsAny: false,
allowDirectConstAssertionInArrowFunctions: true,
allowedNames: [],
allowHigherOrderFunctions: true,
allowTypedFunctionExpressions: true,
},
];

在混合的 JS/TS 程式碼庫中設定

如果你正在處理的程式碼庫中,包含非 TypeScript 程式碼(例如:.js/.mjs/.cjs/.jsx),你應確保使用 ESLint overrides 僅在 .ts/.mts/.cts/.tsx 檔案啟用規則。如果你未這麼做,則你將在 .js/.mjs/.cjs/.jsx 檔案中收到無法修復的程式碼檢查錯誤訊息。

{
"rules": {
// disable the rule for all files
"@typescript-eslint/explicit-module-boundary-types": "off",
},
"overrides": [
{
// enable the rule specifically for TypeScript files
"files": ["*.ts", "*.mts", "*.cts", "*.tsx"],
"rules": {
"@typescript-eslint/explicit-module-boundary-types": "error",
},
},
],
}

allowArgumentsExplicitlyTypedAsAny

此選項為 true 時,該規則將忽略明確指定類型為 any 的引數。

export const func = (value: any): number => value + 1;
在遊樂場中開啟

allowDirectConstAssertionInArrowFunctions

此選項為 true 時,規則會忽略沒有主體的箭頭函式的回傳類型註解,並回傳 as const 類型宣告。

export const func = (value: number) => ({ type: 'X', value });
export const foo = () => ({
bar: true,
});
export const bar = () => 1;
在遊樂場中開啟

allowedNames

你可以傳入不希望此規則忽略的函數/方法名稱,像這樣

{
"@typescript-eslint/explicit-module-boundary-types": [
"error",
{
"allowedNames": ["ignoredFunctionName", "ignoredMethodName"]
}
]
}

allowHigherOrderFunctions

此選項為 true 時,該規則會忽略函式的回傳類型註解,它會立即回傳另一個函式運算式。

export const arrowFn = () => () => {};

export function fn() {
return function () {};
}

export function foo(outer: string) {
return function (inner: string) {};
}
在遊樂場中開啟

allowTypedFunctionExpressions

此選項為 true 時,規則會忽略函式運算式變數的類型註解。

export let arrowFn = () => 'test';

export let funcExpr = function () {
return 'test';
};

export let objectProp = {
foo: () => 1,
};

export const foo = bar => {};
在遊樂場中開啟

何時不使用

如果你的專案並非由對 API 類型敏感的下游消費者使用,你可以停用此規則。

延伸深入閱讀

資源