目录

@typescript-eslint/consistent-type-assertions

强制使用一致的类型断言。

规则配置

// code-linter.json5
{
  "rules": {
    "@typescript-eslint/consistent-type-assertions": "error"
  }
}

选项

详情请参考@typescript-eslint/consistent-type-assertions选项。

正例

// 默认推荐使用 ... 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;

规则集

plugin:@typescript-eslint/all

Code Linter代码检查规则的配置指导请参考代码Code Linter检查。