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で開く// 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;
}
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型として指定された引数を無視します。
- ❌ `allowArgumentsExplicitlyTypedAsAny: false`の場合の正しくない例
- ✅ `allowArgumentsExplicitlyTypedAsAny: true`の場合の正しい例
export const func = (value: any): number => value + 1;
Playgroundで開くexport const func = (value: any): number => value + 1;
Playgroundで開く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;
Playgroundで開くexport const func = (value: number) => ({ type: 'X', value }) as const;
export const foo = () =>
({
bar: true,
}) as const;
export const bar = () => 1 as const;
Playgroundで開く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) {};
}
Playgroundで開くexport const arrowFn = () => (): void => {};
export function fn() {
return function (): void {};
}
export function foo(outer: string) {
return function (inner: string): void {};
}
Playgroundで開く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 => {};
Playgroundで開く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 => {};
Playgroundで開く使用しない場合
プロジェクトがAPI型に影響を受ける可能性のあるダウンストリームのコンシューマーによって使用されていない場合は、このルールを無効にすることができます。
参考資料
- TypeScript 関数