Skip to content

cast-in-test ​

as unknown as T and as never in a test file (named *.test.ts / *.spec.ts, importing a test runner, or in a test, tests or __tests__ directory). An idiomatic rule: reported only with "safe-ts": { "idiomatic": true } in package.json.

Why: a test often needs a value of a big type for one field of it, a fake of a service, or a value of the wrong type on purpose. A cast gives all three, and says nothing about which one it is or which fields the test relies on; when the type gains a required field, the test still compiles. @evobug_com/safe-ts-std/testing names each case: fixture<T>({ ... }), testDouble<T>({ ... }) and unchecked<T>(value). Guide: Testing.

With no-unchecked-cast on, which it is by default, such a cast is reported once, by no-unchecked-cast: in a test file its help names the same helpers. cast-in-test reports where a project turns no-unchecked-cast off for its tests, so the casts there are still named:

jsonc
// package.json
"safe-ts": {
  "idiomatic": true,
  "overrides": [{ "files": ["**/*.test.ts"], "rules": { "no-unchecked-cast": "off" } }]
}

With that setting, the cast below is reported as cast-in-test; with the defaults, as no-unchecked-cast.

✕ Rejectedno-unchecked-cast
ts
import assert from "node:assert/strict";
import { test } from "node:test";

type User = { readonly id: string; readonly name: string; readonly email: string };

function greet(user: User): string {
  return `hello ${user.name}`;
}

test("greets by name", () => {
  assert.equal(greet({ name: "Ada" } as unknown as User), "hello Ada");
});
✓ Accepted0 diagnostics
ts
import assert from "node:assert/strict";
import { test } from "node:test";
import { fixture } from "@evobug_com/safe-ts-std/testing";

type User = { readonly id: string; readonly name: string; readonly email: string };

function greet(user: User): string {
  return `hello ${user.name}`;
}

test("greets by name", () => {
  assert.equal(greet(fixture<User>({ name: "Ada" })), "hello Ada");
});

no-unchecked-cast reports every cast, in tests too, and in a test file its help names the same helpers. This rule is for projects that turn no-unchecked-cast off for tests and still want the two casts that hide the most pointed at the helpers; with both on, such a cast is reported by both.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra