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
コンパイラオプションを使用して、必要な場合にのみ型アノテーションを適用することをお勧めします。
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で開くconst [a]: number[] = [1];
const [b]: [number] = [2];
const [c, d]: [boolean, string] = [true, 'text'];
for (const [key, val] of new Map([['key', 1]])) {
}
Playgroundで開くarrowParameter
アロー関数の引数に型アノテーションを適用するかどうか。
{ "arrowParameter": true }
を使用したコードの例
- ❌ 正しくない
- ✅ 正しい
const logsSize = size => console.log(size);
['hello', 'world'].map(text => text.length);
const mapper = {
map: text => text + '...',
};
Playgroundで開くconst logsSize = (size: number) => console.log(size);
['hello', 'world'].map((text: string) => text.length);
const mapper = {
map: (text: string) => text + '...',
};
Playgroundで開くmemberVariableDeclaration
クラスのメンバ変数に型アノテーションを適用するかどうか。
{ "memberVariableDeclaration": true }
を使用したコードの例
- ❌ 正しくない
- ✅ 正しい
class ContainsText {
delayedText;
immediateTextImplicit = 'text';
}
Playgroundで開くclass ContainsText {
delayedText: string;
immediateTextImplicit: string = 'text';
}
Playgroundで開くobjectDestructuring
オブジェクトデストラクチャリングを使用して宣言された変数に型アノテーションを適用するかどうか。
{ "objectDestructuring": true }
を使用したコードの例
- ❌ 正しくない
- ✅ 正しい
const { length } = 'text';
const [b, c] = Math.random() ? [1, 2] : [3, 4];
Playgroundで開くconst { length }: { length: number } = 'text';
const [b, c]: [number, number] = Math.random() ? [1, 2] : [3, 4];
for (const { key, val } of [{ key: 'key', val: 1 }]) {
}
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で開くfunction logsSize(size: number): void {
console.log(size);
}
const doublesSize = function (size: number): number {
return size * 2;
};
const divider = {
curriesSize(size: number): number {
return size;
},
dividesSize: function (size: number): number {
return size / 2;
},
};
class Logger {
log(text: boolean): boolean {
console.log('>', text);
return true;
}
}
Playgroundで開くpropertyDeclaration
インターフェースと型のプロパティに型アノテーションを適用するかどうか。
{ "propertyDeclaration": true }
を使用したコードの例
- ❌ 正しくない
- ✅ 正しい
type Members = {
member;
otherMember;
};
Playgroundで開くtype Members = {
member: boolean;
otherMember: string;
};
Playgroundで開くvariableDeclaration
配列とオブジェクトのデストラクチャリングを除いて、変数宣言に型アノテーションを適用するかどうか。
{ "variableDeclaration": true }
を使用したコードの例
- ❌ 正しくない
- ✅ 正しい
const text = 'text';
let initialText = 'text';
let delayedText;
Playgroundで開くconst text: string = 'text';
let initialText: string = 'text';
let delayedText: string;
Playgroundで開くvariableDeclarationIgnoreFunction
アロー関数以外の関数とアロー関数の変数宣言を無視します。
{ "variableDeclaration": true, "variableDeclarationIgnoreFunction": true }
を使用したコードの例
- ❌ 正しくない
- ✅ 正しい
const text = 'text';
Playgroundで開くconst a = (): void => {};
const b = function (): void {};
const c: () => void = (): void => {};
class Foo {
a = (): void => {};
b = function (): void {};
c: () => void = (): void => {};
}
Playgroundで開く使用しない場合
特に--noImplicitAny
および/または--strictPropertyInitialization
といった、より厳格なTypeScriptコンパイラオプションを使用している場合は、このルールは必要ない可能性があります。
一般的に、不必要な型アノテーションを書くコストを妥当とみなさない場合は、このルールを使用しないでください。