SKY-T105 · Quality, structure and performance
Unvalidated JSON type assertion
JSON.parse() and a fetch response's .json() return whatever the text contained. Asserting the result with as Settings only tells the compiler what to assume; nothing checks it. SKY-T105 reports such an assertion when no validation sits in between.
- Languages
- TypeScript
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
Why it matters
A settings file edited by hand, or an API that changed its response, is accepted as if it matched the type. The mistake surfaces later as undefined in the middle of the program, or as a value from outside that was trusted without a check.
What Skylos flags
type Settings = { theme: "light" | "dark"; fontSize: number }; export function loadSettings(raw: string): Settings { return JSON.parse(raw) as Settings;}What Skylos leaves alone
type Settings = { theme: "light" | "dark"; fontSize: number }; function isSettings(value: unknown): value is Settings { const v = value as Partial<Record<keyof Settings, unknown>>; const themeOk = v.theme === "light" || v.theme === "dark"; return themeOk && typeof v.fontSize === "number";} export function loadSettings(raw: string): Settings { const data: unknown = JSON.parse(raw); if (!isSettings(data)) { throw new Error("settings file is not valid"); } return data;}Why Skylos leaves it alone
The parsed value is kept as unknown and only becomes Settings after the type guard has checked it. The one assertion left is to a type that still contains unknown, which is not a claim about the data.
More languages
TypeScript
Flagged
type User = { id: string; email: string }; export async function fetchUser(id: string): Promise<User> { const res = await fetch(`/api/users/${id}`); return (await res.json()) as User;}Not flagged
type User = { id: string; email: string }; export async function fetchUser(id: string): Promise<unknown> { const res = await fetch(`/api/users/${id}`); return await res.json();}Why Skylos leaves it alone
The response is returned as unknown, so every caller has to check it before using it as a User.
How Skylos decides
- Runs with
--qualityon.ts,.tsx,.mtsand.ctsfiles. Declaration files, generated files and files that do not parse are skipped. - Finds an assertion (
x as Tor<T>x) to a precise type: notas const, and with noany,unknown,objector{}inside it. - Its operand, looking through parentheses,
await,!andsatisfies, must be either a call to the globalJSON.parsewith one argument that is not a fixed string, or an awaited.json()call with no arguments on a response that comes straight fromawait fetch(...)or from aconstassignedawait fetch(...). - Gives up when
JSON,JSON.parse,fetchorResponse.prototype.jsonis shadowed or reassigned in the file, or when the file is too large to analyse completely. - Reports MEDIUM on the line where the assertion starts.
How to fix it
Keep the parsed value as unknown and check it with a type guard or a schema library such as Zod before using it as the type.
type Settings = { theme: "light" | "dark"; fontSize: number }; Added: function isSettings(value: unknown): value is Settings {Added: const v = value as Partial<Record<keyof Settings, unknown>>;Added: const themeOk = v.theme === "light" || v.theme === "dark";Added: return themeOk && typeof v.fontSize === "number";Added: }Added: export function loadSettings(raw: string): Settings {Removed: return JSON.parse(raw) as Settings;Added: const data: unknown = JSON.parse(raw);Added: if (!isSettings(data)) {Added: throw new Error("settings file is not valid");Added: }Added: return data;}When it can be wrong
- JSON that the program wrote itself a moment earlier, for example a value read back from its own cache, is reported although it cannot have another shape.
- It misses a
.json()call on a response that came from a wrapper such asaxiosor a helper function, and an assertion done in a separate step (const data = JSON.parse(raw); return data as T).
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 JSON.parse(raw) as Settings; // skylos: ignore[SKY-T105] written by saveSettings[tool.skylos] ignore = ["SKY-T105"]inpyproject.tomlturns 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.