@typescript-eslint/consistent-type-assertions
Require one style of type assertion: value as T, <T>value, or none
Also called typescript-eslint/consistent-type-assertions, typescript/consistent-type-assertions
TypeScript has two ways to write a type assertion: value as User and <User>value. You can't use the second one in .tsx files, where it reads as JSX. This rule picks one. By default it's as.
It can also report assertions on object and array literals. { ... } as User compiles when a required property is missing. const user: User = { ... } and { ... } satisfies User don't.
--fix rewrites <T>value to value as T.
Configure#
Off by default. To turn it on:
[lint.rules]
"@typescript-eslint/consistent-type-assertions" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"@typescript-eslint/consistent-type-assertions" = ["error", {
assertionStyle = "as", # "as" or "angle-bracket"
# "allow", "allow-as-parameter" or "never"
objectLiteralTypeAssertions = "allow",
arrayLiteralTypeAssertions = "allow",
}]Examples#
const input = <HTMLInputElement>document.querySelector("input")Use 'as HTMLInputElement' instead of <HTMLInputElement>.;const input = document.querySelector("input") as HTMLInputElement;Options#
| Option | Default | What it does |
|---|---|---|
assertionStyle | "as" | "as" requires value as T. "angle-bracket" requires <T>value. "never" reports every assertion. |
objectLiteralTypeAssertions | "allow" | What to do with { ... } as T. |
arrayLiteralTypeAssertions | "allow" | What to do with [ ... ] as T. |
The last two take "allow", "never", or "allow-as-parameter", which allows the assertion only where there's no variable to annotate: an argument of a call, a default value, a throw, or a value in JSX. They never report as const, as any, or as unknown.
With "never" as the style, as const is still allowed, because it doesn't override the type checker:
{ "assertionStyle": "never" }1 problemconst input = document.querySelector("input") as HTMLInputElementDo not use any type assertions.;{ "assertionStyle": "never" }No problemsconst sizes = ["small", "large"] as const;Object literals:
{ "objectLiteralTypeAssertions": "never" }1 probleminterface User {
name: string;
email: string;
}
const user = { name: "Ada" } as UserAlways prefer const x: T = { ... }.;{ "objectLiteralTypeAssertions": "never" }No problemsinterface User {
name: string;
email: string;
}
const user: User = { name: "Ada", email: "ada@example.com" };
const config = { retries: 3 } as const;
const payload = { name: "Ada" } as unknown;With "allow-as-parameter", the argument is allowed and the variable is reported:
{ "objectLiteralTypeAssertions": "allow-as-parameter" }1 probleminterface User {
name: string;
}
declare function save(user: User): void;
save({ name: "Ada" } as User);
const user = { name: "Ada" } as UserAlways prefer const x: T = { ... }.;Array literals work the same way:
{ "arrayLiteralTypeAssertions": "never" }1 problemconst ports = [3000, 3001] as number[]Always prefer const x: T[] = [ ... ].;{ "arrayLiteralTypeAssertions": "never" }No problemsconst ports: number[] = [3000, 3001];