明確模組邊界類型
在匯出的函式和類別公共類別方法上要求明確傳回和引數的型別。
函式傳回值和引數的明確型別,可讓任何呼叫碼清楚了解模組界限的輸入和輸出。加入這些型別的明確型別註解,有助於提升程式碼可讀性。這也可改善大型程式碼庫中 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;
}
}
在遊樂場中開啟// A function with no return value (void)
export function test(): void {
return;
}
// A return value of type string
export var arrowFn = (): string => 'test';
// All arguments should be typed
export var arrowFn = (arg: string): string => `test ${arg}`;
export var arrowFn = (arg: unknown): string => `test ${arg}`;
export class Test {
// A class method with no return value (void)
method(): void {
return;
}
}
// The function does not apply because it is not an exported function.
function test() {
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 的引數。
- ❌ 對 `allowArgumentsExplicitlyTypedAsAny: false` 錯誤
- ✅ 對 `allowArgumentsExplicitlyTypedAsAny: true` 正確
allowDirectConstAssertionInArrowFunctions
此選項為 true
時,規則會忽略沒有主體的箭頭函式的回傳類型註解,並回傳 as const
類型宣告。
- ❌ 對 `allowDirectConstAssertionInArrowFunctions: false` 錯誤
- ✅ 對 `allowDirectConstAssertionInArrowFunctions: true` 正確
export const func = (value: number) => ({ type: 'X', value });
export const foo = () => ({
bar: true,
});
export const bar = () => 1;
在遊樂場中開啟export const func = (value: number) => ({ type: 'X', value }) as const;
export const foo = () =>
({
bar: true,
}) as const;
export const bar = () => 1 as const;
在遊樂場中開啟allowedNames
你可以傳入不希望此規則忽略的函數/方法名稱,像這樣
{
"@typescript-eslint/explicit-module-boundary-types": [
"error",
{
"allowedNames": ["ignoredFunctionName", "ignoredMethodName"]
}
]
}
allowHigherOrderFunctions
此選項為 true
時,該規則會忽略函式的回傳類型註解,它會立即回傳另一個函式運算式。
- ❌ 對 `allowHigherOrderFunctions: false` 錯誤
- ✅ 對 `allowHigherOrderFunctions: true` 正確
export const arrowFn = () => () => {};
export function fn() {
return function () {};
}
export function foo(outer: string) {
return function (inner: string) {};
}
在遊樂場中開啟export const arrowFn = () => (): void => {};
export function fn() {
return function (): void {};
}
export function foo(outer: string) {
return function (inner: string): void {};
}
在遊樂場中開啟allowTypedFunctionExpressions
此選項為 true
時,規則會忽略函式運算式變數的類型註解。
- ❌ 對 `allowTypedFunctionExpressions: false` 錯誤
- ✅ 對 `allowTypedFunctionExpressions: true` 正確
export let arrowFn = () => 'test';
export let funcExpr = function () {
return 'test';
};
export let objectProp = {
foo: () => 1,
};
export const foo = bar => {};
在遊樂場中開啟type FuncType = () => string;
export let arrowFn: FuncType = () => 'test';
export let funcExpr: FuncType = function () {
return 'test';
};
export let asTyped = (() => '') as () => string;
export let castTyped = <() => string>(() => '');
interface ObjectType {
foo(): number;
}
export let objectProp: ObjectType = {
foo: () => 1,
};
export let objectPropAs = {
foo: () => 1,
} as ObjectType;
export let objectPropCast = <ObjectType>{
foo: () => 1,
};
type FooType = (bar: string) => void;
export const foo: FooType = bar => {};
在遊樂場中開啟何時不使用
如果你的專案並非由對 API 類型敏感的下游消費者使用,你可以停用此規則。
延伸深入閱讀
- TypeScript 函式