メインコンテンツへスキップ

explicit-module-boundary-types

エクスポートされた関数とクラスのパブリッククラスメソッドで、明示的な戻り値と引数の型を要求します。

関数戻り値と引数の明示的な型により、呼び出し側コードに対してモジュール境界の入力と出力が明確になります。これらの型の明示的な型注釈を追加することで、コードの可読性を向上させることができます。また、大規模なコードベースでのTypeScriptの型チェックのパフォーマンスも向上させることができます。

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

Playgroundでこのルールを試してみてください ↗

// 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;
}
}
Playgroundで開く

オプション

このルールは以下のオプションを受け付けます。

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)をlintするコードベースで作業している場合は、ESLintのoverridesを使用して、.ts/.mts/.cts/.tsxファイルでのみルールを有効にする必要があります。有効にしないと、.js/.mjs/.cjs/.jsxファイル内で修正できないlintエラーが報告されます。

{
"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;
Playgroundで開く

allowDirectConstAssertionInArrowFunctions

このオプションがtrueの場合、ルールは、as const型アサーションを返すボディレスアロー関数の戻り値型注釈を無視します。

export const func = (value: number) => ({ type: 'X', value });
export const foo = () => ({
bar: true,
});
export const bar = () => 1;
Playgroundで開く

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) {};
}
Playgroundで開く

allowTypedFunctionExpressions

このオプションがtrueの場合、ルールは関数式の変数の型注釈を無視します。

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

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

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

export const foo = bar => {};
Playgroundで開く

使用しない場合

プロジェクトがAPI型に影響を受ける可能性のあるダウンストリームのコンシューマーによって使用されていない場合は、このルールを無効にすることができます。

参考資料

リソース