# @typescript-eslint/consistent-type-assertions

> Require one style of type assertion: `value as T`, `<T>value`, or none

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`.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"@typescript-eslint/consistent-type-assertions" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"@typescript-eslint/consistent-type-assertions" = ["error", {
  assertionStyle = "as", # "as" or "angle-bracket"
  # "allow", "allow-as-parameter" or "never"
  objectLiteralTypeAssertions = "allow",
  arrayLiteralTypeAssertions = "allow",
}]
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```ts
const input = <HTMLInputElement>document.querySelector("input");
```

Not reported:

{/* good */}
```ts
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:

{/* bad {"options": [{"assertionStyle": "never"}]} */}
```ts
const input = document.querySelector("input") as HTMLInputElement;
```

{/* good {"options": [{"assertionStyle": "never"}]} */}
```ts
const sizes = ["small", "large"] as const;
```

Object literals:

{/* bad {"options": [{"objectLiteralTypeAssertions": "never"}]} */}
```ts
interface User {
  name: string;
  email: string;
}

const user = { name: "Ada" } as User;
```

{/* good {"options": [{"objectLiteralTypeAssertions": "never"}]} */}
```ts
interface 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:

{/* bad {"options": [{"objectLiteralTypeAssertions": "allow-as-parameter"}], "count": 1} */}
```ts
interface User {
  name: string;
}
declare function save(user: User): void;

save({ name: "Ada" } as User);
const user = { name: "Ada" } as User;
```

Array literals work the same way:

{/* bad {"options": [{"arrayLiteralTypeAssertions": "never"}]} */}
```ts
const ports = [3000, 3001] as number[];
```

{/* good {"options": [{"arrayLiteralTypeAssertions": "never"}]} */}
```ts
const ports: number[] = [3000, 3001];
```
