Skip to content

Commit d41792b

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

8 files changed

Lines changed: 257 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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Publish
2+
on:
3+
workflow_dispatch: {}
4+
permissions:
5+
contents: read
6+
id-token: write
7+
jobs:
8+
publish:
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 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` with
7+
`{ assertionStyle: "never" }`, implemented as a native Deno lint plugin.
8+
9+
## Install
10+
11+
Requires Deno 2.2.0 or newer.
12+
13+
```jsonc
14+
// deno.jsonc
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 = response.json() as UserProfile;
51+
```
52+
53+
## License
54+
55+
MIT

deno.jsonc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.jsonc", "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+
"lock": false
20+
}

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`
6+
* with `{ assertionStyle: "never" }`, implemented as a native Deno lint plugin.
7+
*
8+
* @example
9+
* ```jsonc
10+
* // deno.jsonc
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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { assertEquals } 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+
assertEquals(d[0].id, "no-type-assertion/no-type-assertion");
12+
assertEquals(
13+
d[0].message,
14+
"Do not use any type assertions. Use a type annotation or `satisfies` operator instead.",
15+
);
16+
});
17+
18+
Deno.test("reports angle-bracket type assertion", () => {
19+
const d = Deno.lint.runPlugin(
20+
plugin,
21+
"test.ts",
22+
`const x = <string>"hello";`,
23+
);
24+
assertEquals(d.length, 1);
25+
assertEquals(d[0].id, "no-type-assertion/no-type-assertion");
26+
});
27+
28+
Deno.test("allows `as const`", () => {
29+
const d = Deno.lint.runPlugin(
30+
plugin,
31+
"test.ts",
32+
`const x = "hello" as const;`,
33+
);
34+
assertEquals(d.length, 0);
35+
});
36+
37+
Deno.test("allows `<const>`", () => {
38+
const d = Deno.lint.runPlugin(plugin, "test.ts", `const x = <const>"hello";`);
39+
assertEquals(d.length, 0);
40+
});
41+
42+
Deno.test("allows type annotations", () => {
43+
const d = Deno.lint.runPlugin(
44+
plugin,
45+
"test.ts",
46+
`const x: string = "hello";`,
47+
);
48+
assertEquals(d.length, 0);
49+
});
50+
51+
Deno.test("allows `satisfies`", () => {
52+
const d = Deno.lint.runPlugin(
53+
plugin,
54+
"test.ts",
55+
`const x = "hello" satisfies string;`,
56+
);
57+
assertEquals(d.length, 0);
58+
});
59+
60+
Deno.test("reports nested `as` assertion", () => {
61+
const d = Deno.lint.runPlugin(
62+
plugin,
63+
"test.ts",
64+
`const x = (obj as any).foo;`,
65+
);
66+
assertEquals(d.length, 1);
67+
});

0 commit comments

Comments
 (0)