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

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-typeまたはexplicit-module-boundary-typesを使用します。

注意

型アノテーションを不必要に要求すると、保守が面倒になり、一般的にコードの可読性が低下します。TypeScriptは、簡単に記述できる型アノテーションよりも、多くの場合、型をより適切に推論できます。

typedefを有効にする代わりに、一般的には--noImplicitAnyおよび--strictPropertyInitializationコンパイラオプションを使用して、必要な場合にのみ型アノテーションを適用することをお勧めします。

.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で開く

使用しない場合

特に--noImplicitAnyおよび/または--strictPropertyInitializationといった、より厳格なTypeScriptコンパイラオプションを使用している場合は、このルールは必要ない可能性があります。

一般的に、不必要な型アノテーションを書くコストを妥当とみなさない場合は、このルールを使用しないでください。

参考資料

リソース