Skip to content

Commit 4573864

Browse files
committed
feat: ban type assertions (as + angle-bracket), as const allowed
0 parents  commit 4573864

8 files changed

Lines changed: 297 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: CI
2+
on:
3+
push:
4+
branches: [main]
5+
pull_request:
6+
branches: [main]
7+
jobs:
8+
ci:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: denoland/setup-deno@v2
13+
with:
14+
deno-version: v2.x
15+
- run: deno fmt --check
16+
- run: deno lint
17+
- run: deno test
18+
- run: deno publish --dry-run

.github/workflows/publish.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Publish
2+
on:
3+
workflow_dispatch: {}
4+
permissions:
5+
contents: read
6+
id-token: write
7+
jobs:
8+
publish:
9+
if: github.ref == 'refs/heads/main'
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: denoland/setup-deno@v2
14+
with:
15+
deno-version: v2.x
16+
- run: deno publish

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
coverage/
2+
.DS_Store

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 Hiro5409
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# @hiro5409/deno-no-type-assertion-lint
2+
3+
Deno lint plugin that forbids TypeScript type assertions (`as` and angle-bracket
4+
syntax). `as const` / `<const>` is allowed.
5+
6+
Inspired by `@typescript-eslint/consistent-type-assertions` (`assertionStyle: "never"`)
7+
and `oxc/consistent-type-assertions` (error message), implemented as a native Deno lint plugin.
8+
9+
## Install
10+
11+
Requires Deno 2.2.0 or newer.
12+
13+
```jsonc
14+
// deno.json
15+
{
16+
"lint": {
17+
"plugins": ["jsr:@hiro5409/deno-no-type-assertion-lint"]
18+
}
19+
}
20+
```
21+
22+
## Why?
23+
24+
Type assertions tell TypeScript to trust you instead of proving the type. They
25+
can hide real type errors, especially around object literals, DOM APIs, and
26+
untyped data.
27+
28+
This rule nudges code toward:
29+
30+
- explicit type annotations
31+
- `satisfies` for shape checking
32+
- type guards when runtime narrowing is actually needed
33+
34+
## Examples
35+
36+
```typescript
37+
// Bad
38+
const canvas = document.getElementById("c") as HTMLCanvasElement;
39+
const name = <string> data.name;
40+
41+
// Good
42+
const canvas: HTMLCanvasElement | null = document.querySelector("canvas");
43+
const config = { mode: "dark" } satisfies AppConfig;
44+
```
45+
46+
## Ignoring a report
47+
48+
```typescript
49+
// deno-lint-ignore no-type-assertion/no-type-assertion -- API returns untyped JSON
50+
const data = await response.json() as UserProfile;
51+
```
52+
53+
## License
54+
55+
MIT

deno.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "@hiro5409/deno-no-type-assertion-lint",
3+
"version": "0.1.0",
4+
"license": "MIT",
5+
"exports": "./mod.ts",
6+
"publish": {
7+
"include": ["LICENSE", "README.md", "deno.json", "mod.ts"]
8+
},
9+
"imports": {
10+
"@std/assert": "jsr:@std/assert@1"
11+
},
12+
"tasks": {
13+
"test": "deno test",
14+
"check": "deno publish --dry-run"
15+
},
16+
"lint": {
17+
"plugins": ["./mod.ts"]
18+
},
19+
"compilerOptions": {
20+
"noUncheckedIndexedAccess": true,
21+
"exactOptionalPropertyTypes": true,
22+
"noPropertyAccessFromIndexSignature": true,
23+
"noImplicitOverride": true,
24+
"noFallthroughCasesInSwitch": true,
25+
"erasableSyntaxOnly": true
26+
},
27+
"lock": false
28+
}

mod.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Deno lint plugin that forbids TypeScript type assertions (`as` and angle-bracket).
3+
* `as const` / `<const>` is allowed.
4+
*
5+
* Inspired by `@typescript-eslint/consistent-type-assertions` (`assertionStyle: "never"`)
6+
* and `oxc/consistent-type-assertions` (error message), implemented as a native Deno lint plugin.
7+
*
8+
* @example
9+
* ```jsonc
10+
* // deno.json
11+
* {
12+
* "lint": {
13+
* "plugins": ["jsr:@hiro5409/deno-no-type-assertion-lint"]
14+
* }
15+
* }
16+
* ```
17+
*
18+
* @module
19+
*/
20+
21+
/** Whether a type annotation node is `const` (for `as const` / `<const>`). */
22+
function isConst(
23+
ta: { type: string; typeName?: { type: string; name?: string } },
24+
): boolean {
25+
return ta.type === "TSTypeReference" &&
26+
ta.typeName?.type === "Identifier" &&
27+
ta.typeName.name === "const";
28+
}
29+
30+
const MESSAGE =
31+
"Do not use any type assertions. Use a type annotation or `satisfies` operator instead.";
32+
33+
/**
34+
* The lint plugin instance.
35+
*
36+
* Rules:
37+
* - `no-type-assertion/no-type-assertion` — Bans type assertions (`as const` / `<const>` allowed)
38+
*/
39+
const plugin: Deno.lint.Plugin = {
40+
name: "no-type-assertion",
41+
rules: {
42+
"no-type-assertion": {
43+
create(context) {
44+
return {
45+
TSAsExpression(node) {
46+
if (isConst(node.typeAnnotation)) return;
47+
context.report({ node, message: MESSAGE });
48+
},
49+
TSTypeAssertion(node) {
50+
if (isConst(node.typeAnnotation)) return;
51+
context.report({ node, message: MESSAGE });
52+
},
53+
};
54+
},
55+
},
56+
},
57+
};
58+
59+
export default plugin;

mod_test.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { assertEquals, assertExists } from "@std/assert";
2+
import plugin from "./mod.ts";
3+
4+
Deno.test("reports `as` type assertion", () => {
5+
const d = Deno.lint.runPlugin(
6+
plugin,
7+
"test.ts",
8+
`const x = "hello" as unknown;`,
9+
);
10+
assertEquals(d.length, 1);
11+
const diag = d[0];
12+
assertExists(diag);
13+
assertEquals(diag.id, "no-type-assertion/no-type-assertion");
14+
assertEquals(
15+
diag.message,
16+
"Do not use any type assertions. Use a type annotation or `satisfies` operator instead.",
17+
);
18+
});
19+
20+
Deno.test("reports angle-bracket type assertion", () => {
21+
const d = Deno.lint.runPlugin(
22+
plugin,
23+
"test.ts",
24+
`const x = <string>"hello";`,
25+
);
26+
assertEquals(d.length, 1);
27+
const diag = d[0];
28+
assertExists(diag);
29+
assertEquals(diag.id, "no-type-assertion/no-type-assertion");
30+
});
31+
32+
Deno.test("allows `as const`", () => {
33+
const d = Deno.lint.runPlugin(
34+
plugin,
35+
"test.ts",
36+
`const x = "hello" as const;`,
37+
);
38+
assertEquals(d.length, 0);
39+
});
40+
41+
Deno.test("allows `<const>`", () => {
42+
const d = Deno.lint.runPlugin(plugin, "test.ts", `const x = <const>"hello";`);
43+
assertEquals(d.length, 0);
44+
});
45+
46+
Deno.test("allows type annotations", () => {
47+
const d = Deno.lint.runPlugin(
48+
plugin,
49+
"test.ts",
50+
`const x: string = "hello";`,
51+
);
52+
assertEquals(d.length, 0);
53+
});
54+
55+
Deno.test("allows `satisfies`", () => {
56+
const d = Deno.lint.runPlugin(
57+
plugin,
58+
"test.ts",
59+
`const x = "hello" satisfies string;`,
60+
);
61+
assertEquals(d.length, 0);
62+
});
63+
64+
Deno.test("reports nested `as` assertion", () => {
65+
const d = Deno.lint.runPlugin(
66+
plugin,
67+
"test.ts",
68+
`const x = (obj as any).foo;`,
69+
);
70+
assertEquals(d.length, 1);
71+
});
72+
73+
Deno.test("reports double assertion (as unknown as T)", () => {
74+
const d = Deno.lint.runPlugin(
75+
plugin,
76+
"test.ts",
77+
`const x = value as unknown as string;`,
78+
);
79+
assertEquals(d.length, 2);
80+
});
81+
82+
Deno.test("reports generic angle-bracket assertion", () => {
83+
const d = Deno.lint.runPlugin(
84+
plugin,
85+
"test.ts",
86+
`const x = <Array<string>>value;`,
87+
);
88+
assertEquals(d.length, 1);
89+
});
90+
91+
Deno.test("allows `as const satisfies`", () => {
92+
const d = Deno.lint.runPlugin(
93+
plugin,
94+
"test.ts",
95+
`const x = { a: 1 } as const satisfies { a: number };`,
96+
);
97+
assertEquals(d.length, 0);
98+
});

0 commit comments

Comments
 (0)