typescript-type-safety
Skill1 starsChanged 2 months agoMITView on GitHub
Type-level correctness that scales past the basics - discriminated unions with exhaustive never checks, avoiding any, narrowing with type guards, satisfies vs as, generic constraints, and validating untrusted input at the boundary because types erase at runtime. Use when modelling a state machine, an API response, or an event stream, or when a user says "how do I model this union", "exhaustiveness", "should I use as or satisfies", "type is wrong at runtime", or when casting creeps into a codebase. Triggers on "any", "as const", "satisfies", "never", "type guard", "discriminated union", "narrowing", "type-safe", "unvalidated JSON".
3 other projects use a version of this.
Adopted by
Most recently taken up by
- catesandrew/skillstheir own version, first seen Nov 1, 2026
- theonly1me/shadowclonetheir own version, first seen Oct 11, 2026
- pr-pm/prpmtheir own version, first seen Feb 24, 2026
Other versions3 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 3
- pr-pm/prpm★ 124same name
- theonly1me/shadowclone★ 31same name
- catesandrew/skills★ 1same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository126 other files it gives its agents.
- eu-data-backend-job-hunterSkill
- job-application-matcherSkill
- llm-agents-and-tool-useSkill
- llm-application-architectureSkill
- llm-cost-and-latency-optimizationSkill
- llm-evaluationSkill
- llm-fine-tuning-and-adaptersSkill
- llm-privacy-and-safetySkill
- llm-prompt-engineeringSkill
- llm-rag-systemsSkill
- structured-output-and-jsonSkill
- graphql-api-designSkill
- pagination-and-filteringSkill
- rest-api-designSkill
- webhooks-and-integrationsSkill
- caching-architecture-multi-layerSkill
- resilience-patternsSkill
- service-boundaries-and-monolithsSkill
- system-design-interviewSkill
- go-concurrencySkill
- go-error-handlingSkill
- java-spring-patternsSkill
- nodejs-server-performanceSkill
- python-async-patternsSkill
- python-data-modelingSkill
- python-packaging-and-toolingSkill
- python-performanceSkill
- rest-api-implementationSkill
- rust-error-handlingSkill
- rust-ownership-patternsSkill
- ci-caching-and-speedupSkill
- ci-pipeline-designSkill
- deployment-strategiesSkill
- github-actions-hardeningSkill
- release-versioning-and-changelogsSkill
- reproducible-buildsSkill
- aws-iam-and-securitySkill
- aws-serverless-architectureSkill
- azure-fundamentalsSkill
- cloud-cost-optimizationSkill
- cloud-networking-and-vpcSkill
- gcp-cloud-architectureSkill
- code-review-skillsSkill
- design-review-and-refactoring-architectureSkill
- refactoring-techniqueSkill
- understanding-unknown-codebaseSkill
- buildkit-and-cacheSkill
- container-image-debuggingSkill
- container-runtime-securitySkill
- docker-compose-workflowsSkill
- dockerfile-authoringSkill
- docker-image-optimizationSkill
- database-connection-managementSkill
- database-transactionsSkill
- distributed-databasesSkill
- nosql-data-modelingSkill
- postgres-query-tuningSkill
- redis-and-cachingSkill
- schema-migrationsSkill
- data-modeling-for-analyticsSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
3 rules. 0 are common, 0 are distinctive, and 3 are very specific to this project.
0
Common
Most of the 3 edited versions we read keep these.
0
Distinctive
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 115
Prefer, in order: unknown > a precise union > a generic <T> constrained to the shape > any.
- Very specific: line 162
Prefer asserts x is T (assertion function) at a parse/validate boundary, and x is T (guard) for branching.
- Very specific: line 274
Excess property checking: only applies to a fresh object literal assigned to a typed target.
“Common” is measured: kept by at least 6 in 10 of the 3 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
TypeScript Type Safety
Types in TypeScript erase at runtime. They are a compile-time tool plus a
documentation and editor aid; a User from fetch().json() is a claim, not
a fact. The way to be actually type-safe is two-layered: model the domain with
precise types (discriminated unions, no any), and validate untrusted data
at the edge before it enters the typed world. This skill covers the types;
see the runtime boundary section for the validation.
Workflow
- 1. Model states as a discriminated union with a literal
type/kindfield - 2. Handle every variant with an
assertNever/neverreturn so the compiler catches a missed case - 3. Keep
anyout; useunknownat the boundary and narrow - 4. Use
satisfiesto check a shape without widening it; reserveasfor genuine downcasts you can justify - 5. Validate at the edge (schema) and let types flow inward
- 6. Turn on
strict,noUncheckedIndexedAccess,exactOptionalPropertyTypes
Discriminated unions: the workhorse
A union of object types sharing a literal discriminant field is
discriminated. switch on it is checked, and adding a variant produces a
compile error at every switch that did not handle it. This is how you make
"impossible states" unrepresentable in TypeScript.
type RequestState =
| { status: 'idle' }
| { status: 'loading'; startedAt: number }
| { status: 'success'; data: User; etag: string }
| { status: 'error'; error: ApiError; retryable: boolean };
function render(s: RequestState): string {
switch (s.status) { // s is narrowed per case
case 'idle': return 'nothing yet';
case 'loading': return `loading since ${s.startedAt}`; // startedAt exists
case 'success': return s.data.name; // data exists
case 'error': return s.error.message; // error exists
default: return assertNever(s); // catches new variants
}
}
function assertNever(x: never): never {
throw new Error(`Unhandled variant: ${JSON.stringify(x)}`);
}assertNever(s)in thedefaultis the whole point. The moment you add a{ status: 'cancelled' }variant, everyswitchthat ends inassertNeverfails to compile until you handle it. Without it, a missing case silently returnsundefinedat runtime.neveris also the return type of a function that never returns; passing the narrowed leftover to it is the exhaustiveness check. TS'snoImplicitReturnsplus this pattern is the complete solution.- Discriminant field names:
status,type,kind,tag— pick one and be consistent. It must be a literal type, notstring, or the union does not discriminate:type Bad = { status: string; data: User } | { status: string; error: E }; // no discrimination: s.data does not exist in the error branch - Optional discriminant:
{ status?: 'idle' }breaks the discrimination (the discriminant must be present in every member). Make it required. - Discriminated unions are the answer to "optional fields of different shapes"
— instead of
data?: User; error?: ApiError; loading?: boolean(which permits all three at once and none of them), use the union above (which permits exactly the legal combinations).
Exhaustiveness beyond switch
assertNever is the pattern; where it goes varies:
- Switch statement (above) — the default case.
- Array
.mapover variants — a lookup object keyed by discriminant is exhaustive only if typed as a mapped type:const labels: Record<RequestState['status'], string> = { idle: 'Nothing yet', loading: 'Loading', success: 'Done', error: 'Failed', }; // add a variant -> this object is missing a key -> compile errorRecord<Union, T>(or asatisfiesobject) is the "no new key silently ignored" version. A bare object literal is not checked for exhaustiveness unless you annotate it asRecord<...>. - A catch-all
defaultthat does NOT callassertNeversilently swallows new variants. That is the trap.
Never any
any is contagious: one any in a position disables checking for everything
that flows through it, silently. unknown is the type-safe "I do not know
yet" — it forces you to narrow before use.
// Bad: json is any; data.name and data.id are unchecked.
const data = (await res.json()); // any
// Good: unknown, then narrow (see guards below).
const data: unknown = await res.json();anyis acceptable in exactly two places: a value you are narrowing in the next line (const x = y as any; if (isFoo(x)) ...is still a cast), and a deliberate escape hatch with a comment. Everywhere else it is a bug.- Prefer, in order:
unknown> a precise union > a generic<T>constrained to the shape >any. // @ts-expect-error(not@ts-ignore) — it errors if the next line is actually fine, so it goes stale loudly when the code is fixed.@ts-ignoresilently suppresses forever. Use it as a temporary marker with an issue link, and drive the count to zero.strict: trueintsconfig.jsonenablesnoImplicitAny,strictNullChecks,strictFunctionTypes, etc.strictNullChecksis the one that catches the most real bugs (string | undefinedvsstring).noUncheckedIndexedAccess: truemakesarr[0]beT | undefined(arrays can be sparse/out of bounds). It is stricter but it models reality and catches real off-by-one bugs. Same value for correctness.
Narrowing and type guards
Narrowing is how unknown becomes a real type. Techniques, from narrowest to
broadest:
// 1. typeof / instanceof / in — built in
if (typeof x === 'string') { x.toUpperCase(); } // x: string
if (x instanceof Error) { x.message; }
if ('name' in x) { x.name; }
// 2. Discriminant check on a union
if (state.status === 'success') { state.data; }
// 3. User-defined type guard — the workhorse for API/domain types
function isUser(x: unknown): x is User {
return typeof x === 'object' && x !== null
&& typeof (x as any).id === 'string'
&& typeof (x as any).name === 'string';
}
// 4. Assertion function (throws, narrows) — good for validated boundaries
function assertUser(x: unknown): asserts x is User {
if (!isUser(x)) throw new ValidationError('not a User');
}
function render(u: unknown) {
if (isUser(u)) return u.name; // u: User here
return 'unknown';
}- A type predicate is
x is User; the function body must actually check it (TypeScript trusts you — a guard that lies is unsound and a bug). - Prefer
asserts x is T(assertion function) at a parse/validate boundary, andx is T(guard) for branching. innarrowing works for union members ('data' in snarrows to the branch withdata) and, since TS 4.9, for unlisted-property checks on object types.- Narrowing does not survive assignment to a
letof a wider type or through a closure that runs later, in some cases; when narrowing "does not work", the fix is usually to assign to aconstin the narrowed branch. - For
unknownreaching a function expecting a type, do not cast — make the function acceptunknownand narrow inside, or narrow at the call site with a guard.
satisfies vs as
asis a claim / assertion / cast. It tells the compiler "trust me", produces no error if you are wrong, and is unchecked at runtime. Use it for a genuine downcast (anunknownyou have just narrowed, a widening for a legacy API), never to silence an error you have not understood.satisfies(4.9+) checks a value against a type without changing the value's inferred type. This is the fix for the classic "I annotated and lost my literal types" problem.
const routes = {
home: { method: 'GET', auth: false },
create: { method: 'POST', auth: true },
};
// `as const` widens nothing but freezes; `satisfies` checks without freezing:
const routes = {
home: { method: 'GET', auth: false },
create: { method: 'POST', auth: true },
} satisfies Record<string, { method: 'GET'|'POST'; auth: boolean }>;
// routes.home.method is still the literal 'GET' (not 'GET'|'POST')
// and a typo (`method: 'DELTE'`) is a compile error.
const theme = { primary: '#0af' } satisfies Record<string, string>;
theme.primary // still '#0af' (literal), not stringas constmakes a value deeply readonly and literal.satisfiesvalidates shape and keeps inference. Useas constfor arrays/objects you want frozen and literal; usesatisfiesfor config objects you want checked against a type but still inferred precisely. They compose:{...} as const satisfies Config.as conston a wide object can make it painful (everything readonly and literal);satisfiesis the gentler, more common tool in app code.
Generics and constraints
- Constrain a generic to the operations you use.
<T extends { id: string }>means the body may call.idand the compiler enforces it for every instantiation. consttype parameters (5.0+) make a generic infer a literal:
Withfunction get<K extends PropertyKey>(obj: Record<K, unknown>, key: K): Record<K, unknown>[K] { return obj[key]; }const, passing'a' as constor a literal infers the literal type, notstring. This replaces manyas consthacks.NoInfer<T>(5.4+) blocks inference at that parameter so a default controls it — useful forfunction f<T>(v: T, fallback: NoInfer<T>).extendson a generic constrains what can be passed, not the shape of the output. If you need the output to keep the specific subtype, returnT, notSomeBase<T>.
The runtime-vs-type boundary (the non-negotiable rule)
Types are erased. JSON.parse returns any; a cast does not check
anything. The moment data crosses from outside your program — a fetch
response, a query param, a file, localStorage, a message, a WebSocket frame
— it is unknown and must be validated at runtime. A type annotation on
untrusted data is a comment that lies.
// The lie: this is a claim, not a fact.
const user = (await fetch('/api/user').then(r => r.json())) as User;
// The truth: validate, then the type is earned.
const body: unknown = await fetch('/api/user').then(r => r.json());
if (!isUser(body)) throw new ValidationError('malformed user');Validate at the edge and let validated types flow inward:
- Pick one validator: Zod (schema doubles as a type via
z.infer), or Valibot, or hand-written type guards for small shapes. Schema-first (Zod) means the runtime check and the static type cannot drift. - Validate every network response, every env var, every parsed cookie/JWT
payload, every
localStorageread, and every message off a queue or WebSocket. Assume every one of these can be wrong or hostile. - Set
compilerOptions.strictand keep the boundary schema. The type system stops you misusing your own data; only validation stops you being fed wrong data.
import { z } from 'zod';
const UserSchema = z.object({
id: z.string(),
name: z.string(),
age: z.number().int().nonnegative(),
});
type User = z.infer<typeof UserSchema>; // the type comes FROM the schema
const raw: unknown = await res.json();
const user = UserSchema.parse(raw); // throws ZodError with a path if badSee python-data-modeling for the same boundary discipline in Python, and
rest-api-implementation for the response validation side.
Gotchas
- Excess property checking only applies to a fresh object literal
assigned to a typed target.
{ a: 1, b: 2 }assigned to{ a: number }errors, but a variable (or a spread) does not get the excess-property check. It is why "it worked before I refactored" for object literals. as constis not validation. It only tells the compiler; it does not check anything at runtime and it does not deep-freeze in JS.innarrowing andObject.hasOwndiffer for inherited properties; useObject.hasOwn(o, 'k')(ES2022) when you care.- Optional properties and
exactOptionalPropertyTypes: with it on,{ a?: string }does not accept{ a: undefined }explicitly; without it, it does. Turning it on is stricter and catches real bugs but breaks code that passesprop: undefined— decide per project, document it. - Readonly arrays:
readonly T[]will not accept aT[]in every position, andreadonlyis shallow.as constgivesreadonlydeeply but freezes literals you may want to pass as mutable. - Union of object types is a poor substitute for a class with methods — the discriminant gives you data safety, not behaviour. Keep logic in functions that switch on the discriminant, not methods on the variants.
unknownin a generic position without a constraint propagatesunknowneverywhere and is as unusable asany. Constrain it (<T extends object>) or narrow it.- Do not use type predicates to lie.
function isFoo(x: unknown): x is Foo { return true; }compiles and is a bug waiting to happen.
House defaults
strict: trueplusnoUncheckedIndexedAccessandexactOptionalPropertyTypesintsconfig.json.- Discriminated unions for every state machine / event / API response;
assertNeverin every exhaustive switch. unknownat every boundary, narrowed with guards; zeroanyin app code;@ts-expect-erroronly as a temporary, tracked marker.satisfiesto check config shapes without widening;asonly for deliberate, commented downcasts.- One schema validator (Zod by default) at every untrusted edge; the static type is derived from the schema, never asserted onto raw data.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

