Skip to main content

SKY-T103 · Quality, structure and performance

Suspicious chained type assertion

Medium

A type assertion (value as User) tells the TypeScript compiler to trust you about a value's type; it checks nothing at run time. Chaining two assertions through unknown or any (value as unknown as User) forces a type the compiler would otherwise reject. SKY-T103 reports such a chain when it ends in a precise type.

Languages
TypeScript
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

The compiler stops checking, so if the value does not have that shape the error appears later and somewhere else: user.isAdmin is undefined, the admin check quietly fails, or a missing field crashes a page far from the line that caused it.

What Skylos flags

src/auth/session.ts · TypeScriptReported on line 4
type User = { id: string; email: string; isAdmin: boolean }; export function currentUser(session: { user: object }): User {  return session.user as unknown as User;}

What Skylos leaves alone

src/auth/session.ts · TypeScript
type User = { id: string; email: string; isAdmin: boolean }; function isUser(value: unknown): value is User {  const v = value as Record<string, unknown>;  return (    typeof v.id === "string" &&    typeof v.email === "string" &&    typeof v.isAdmin === "boolean"  );} export function currentUser(session: { user: object }): User {  if (!isUser(session.user)) {    throw new Error("session has no valid user");  }  return session.user;}

Why Skylos leaves it alone

The only assertion is a single step to Record<string, unknown>, a type that still says "unknown", inside a type guard that checks every field at run time. Skylos reports only a chain whose last type is precise.

How Skylos decides

  1. Runs with --quality on .ts, .tsx, .mts and .cts files. Declaration files (.d.ts) and generated files (under build, dist, generated or vendor folders, *.gen.ts or *.generated.ts names, or with a generated-code header) are skipped, and so is code inside a part of the file that does not parse.
  2. Finds a type assertion, written x as T or <T>x, whose operand is itself an assertion, looking through parentheses, await, ! and satisfies.
  3. The inner assertion must be to a bridge type: unknown, any, object or {}.
  4. The outer type must be precise: not as const, and with no any, unknown, object or {} anywhere inside it. x as unknown as Record<string, unknown> is therefore not reported.
  5. Reports MEDIUM on the line where the outer assertion starts.

How to fix it

Check the value at run time with a type guard (or a schema library such as Zod) and let the compiler narrow it, instead of asserting it.

src/auth/session.ts · TypeScript · the fix, against the flagged file
type User = { id: string; email: string; isAdmin: boolean }; Added: function isUser(value: unknown): value is User {Added:   const v = value as Record<string, unknown>;Added:   return (Added:     typeof v.id === "string" &&Added:     typeof v.email === "string" &&Added:     typeof v.isAdmin === "boolean"Added:   );Added: }Added:  export function currentUser(session: { user: object }): User {Removed:   return session.user as unknown as User;Added:   if (!isUser(session.user)) {Added:     throw new Error("session has no valid user");Added:   }Added:   return session.user;}

When it can be wrong

  • A chain is sometimes the only way to bridge two library types that describe the same object, for example two versions of a generated client. It is still reported.
  • A single assertion such as data as User hides the same problem but is not reported by this rule.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • On the reported line:

    TypeScript
    return session.user as unknown as User; // skylos: ignore[SKY-T103] auth checked it
  • [tool.skylos] ignore = ["SKY-T103"] in pyproject.toml turns the rule off for the project. A baseline accepts existing findings, but it records the absolute file path and the line, so an entry stops matching when the checkout moves or the line shifts.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.