{"success":true,"name":"typescript-as-silences-the-compiler-it-doesn-t-fix-the-data-","description":"Writing TypeScript and about to use 'as SomeType' to make an error go away","category":"ai-brimind","path":"ai-brimind/typescript-as-silences-the-compiler-it-doesn-t-fix-the-data-/SKILL.md","file":"SKILL.md","content":"---\nname: typescript-as-silences-the-compiler-it-doesn-t-fix-the-data-\ndescription: \"Writing TypeScript and about to use 'as SomeType' to make an error go away\"\n---\n\n# TypeScript 'as' Silences the Compiler, It Doesn't Fix the Data: Assert Only What You Verified\n\n## When to use\nProject is TypeScript, and you're about to write 'as Type', 'as any', or a non-null '!' to satisfy the compiler about data you haven't actually checked.\n\n## Example request\nfetch result cast 'as User' → the API changed a field and the crash happens far from the cast; the assertion promised something nobody verified. — 'as any as OtherType' double-cast to force a conversion → the compiler's objection was the real signal that the shapes don't match. — value! non-null assertion on something that IS null at runtime → the crash lands exactly where the compiler warned; the '!' deleted the warning, not the null. — Cast replaced with a type guard or schema check at the boundary → the type is now EARNED from a runtime check; verification is the honest form of assertion.\n\n## Instructions\nAct as a customs officer who knows a stamp on the box doesn't change what's inside: 'as Type' is a stamp, not an inspection. When tempted to assert a type: 1) Read what the compiler is actually objecting to and classify it — the data genuinely might not match (most common), the type definition is wrong, or TypeScript just can't follow logic you know is sound; only the last is legitimate assertion territory, and it should be rare. 2) For data from outside (API responses, JSON.parse, storage, user input), never assert — verify: a type guard or schema validation at the boundary EARNS the type from a runtime check, and everything downstream is genuinely safe instead of hopefully safe. 3) For non-null '!': ask what actually guarantees the value exists at this line — if the guarantee is real, often a plain if-check expresses it and narrows the type honestly; if you can't name the guarantee, the '!' is a scheduled crash. 4) Treat double-casts (as any as X) as an alarm: the shapes don't overlap and the compiler said so — write the actual conversion between the two shapes instead of forging the paperwork. 5) When a wrong library type definition forces assertions, fix or override the definition in ONE place rather than scattering casts at every call site. If you can't say out loud WHY the value has the asserted type, you don't know it does — check the runtime reality first. Only keep an assertion once the guarantee behind it is confirmed and stated, and explain why the type is true rather than merely quiet.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #978\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}