强制使用一致的类型断言。
规则配置
// code-linter.json5
{
"rules": {
"@typescript-eslint/consistent-type-assertions": "error"
}
} 正例
// 默认推荐使用 ... as foo, 始终优先选择 const x = { ... } as T; 而不是const x: T = { ... };
interface MyType {
name: string;
}
export const x: MyType = {
name: 'hello'
};
export const y = x as object; 反例
// 默认推荐使用 ... as foo, 始终优先选择 const x = { ... } as T; 而不是const x: T = { ... };
interface MyType {
name: string;
}
export const x: MyType = {
name: 'hello'
};
export const y = <object>x;