typescript-strict-patterns
Skill2 starsChanged 4 months agoMITView on GitHub
Use when writing or reviewing any TypeScript code. Covers discriminated unions, branded types, Zod at boundaries, const arrays over enums, and safe access patterns.
In this repository20 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- adopt-agentic-workflowSkill
- branch-and-commitSkill
- byeSkill
- commit-notationSkill
- commitSkill
- dedicated-browserSkill
- handling-pull-requestsSkill
- jest-testing-conventionsSkill
- markdownSkill
- pnpmSkill
- reality-checkSkill
- schweizer-schreibweiseSkill
- show-your-workSkill
- test-driven-developmentSkill
- triage-ticketSkill
- working-with-bitbucket-apiSkill
- working-with-bitbucket-webSkill
- working-with-jira-webSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
13 rules. About 0 are common, 2 are distinctive, and 11 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
11
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 41
Use satisfies never or the assertNever helper at the default: branch.
- Very specific: line 80
Avoid non-null assertions (!) and type assertions (as) in production code — they hide type errors.
- Very specific: line 83
Destructuring with defaults: instead of obj.prop!: const { name = '' } = config.
- Very specific: line 84
.at()+ nullish coalescing: instead of arr[0]!: const first = arr.at(0) ?? - Very specific: line 85
Guard clause: instead of value as Foo: narrow with a type guard, then the type flows naturally.
- Very specific: line 89
Never use enum (enforced by ESLint).
- Very specific: line 100
Use Zod schemas as the single source of truth for data crossing system boundaries (disk I/O, env vars, API responses, config files).
- Very specific: line 110
Use safeParse() for data that may be corrupt (disk reads, JSONL) — skip gracefully
- Very specific: line 111
Use parse() for startup validation (env vars) where failure is fatal
- Very specific: line 118
Use .at(index) — clearer intent than bracket access
- Very specific: line 120
Prefer .find(), .filter(), or destructuring over index access
- Distinctive: line 128
If you encounter a pattern that doesn't work with newer TypeScript versions, a missing pattern, or incorrect guidance in this skill, don't …
- Distinctive: line 133
Never silently work around a skill gap.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
TypeScript Strict Patterns
Project setup — read
project-setup.mdwhen bootstrapping a new project or changing tsconfig / ts-reset / type-fest. ESLint baseline — readeslint.config.mjswhen adding or tweaking lint rules.
Discriminated Unions + Exhaustive Checking
Model variants as discriminated unions — never bags of optional properties:
// GOOD — each variant carries exactly its data
type Result =
| { status: "ok"; data: string }
| { status: "error"; message: string };
// Exhaustive check helper — will fail to compile if a variant is missed
function assertNever(x: never): never {
throw new Error(`Unexpected: ${JSON.stringify(x)}`);
}
function handle(r: Result) {
switch (r.status) {
case "ok": return r.data;
case "error": return r.message;
default: assertNever(r); // compile error if a case is missing
}
}Use satisfies never or the assertNever helper at the default: branch. ESLint's switch-exhaustiveness-check enforces this at lint time.
Branded Types
Prevent accidental interchange of structurally identical types with a brand:
type Brand<T, B extends string> = T & { readonly __brand: B };
type UserId = Brand<string, "UserId">;
type OrderId = Brand<string, "OrderId">;
function getUser(id: UserId) { /* ... */ }
const uid = "abc" as UserId; // cast once at the boundary
getUser(uid); // OK
getUser("abc"); // compile error — plain string is not UserIdBrand at system boundaries (API response parsing, DB reads). Internal code then carries the brand without further casts.
Template Literal Types
Enforce string formats at the type level:
type HexColor = `#${string}`;
type Route = `/${string}`;
type EventName = `on${Capitalize<string>}`;
function setColor(c: HexColor) { /* ... */ }
setColor("#ff0000"); // OK
setColor("red"); // compile errorUseful for config keys, route paths, and event names where runtime validation is overkill but typos are common.
No ! or as in Production Code
Avoid non-null assertions (!) and type assertions (as) in production code — they hide type errors. The one sanctioned as is the brand cast at a validated boundary (see Branded Types). ! is lint-enforced (no-non-null-assertion, relaxed in *.test.ts/*.spec.ts); as is enforced by review.
Replacements:
- Destructuring with defaults instead of
obj.prop!:const { name = '' } = config; .at()+ nullish coalescing instead ofarr[0]!:const first = arr.at(0) ?? fallback;- Guard clause instead of
value as Foo: narrow with a type guard, then the type flows naturally.
Const Arrays Over Enums
Never use enum (enforced by ESLint). Use as const arrays with derived types:
const STATUSES = ["pending", "active", "done"] as const;
type Status = (typeof STATUSES)[number];At system boundaries where Zod already validates, prefer z.enum(STATUSES) — it gives you the union type and runtime validation in one step.
Zod Schemas at System Boundaries
Use Zod schemas as the single source of truth for data crossing system boundaries (disk I/O, env vars, API responses, config files). Derive types with z.infer<> — never duplicate a hand-written interface alongside a schema.
export const sessionMetaSchema = z.object({
token: z.string(),
status: z.enum(["running", "completed", "error"]),
});
export type SessionMeta = z.infer<typeof sessionMetaSchema>;- Use
safeParse()for data that may be corrupt (disk reads, JSONL) — skip gracefully - Use
parse()for startup validation (env vars) where failure is fatal - Skip Zod for internal function arguments between trusted modules and for SDK-owned types
Safe Indexed Access
With noUncheckedIndexedAccess, bracket access returns T | undefined. Always narrow:
- Use
.at(index)— clearer intent than bracket access - Handle with
if (item !== undefined)or?? - Prefer
.find(),.filter(), or destructuring over index access
Type Helpers (type-fest as Inspiration)
Use type-fest as a reference catalog when strict patterns make code verbose. Browse its source for solutions like SetRequired, Simplify, JsonValue. Copy the single type definition you need into src/types/ with attribution. Don't add the full package as a dependency — keep the dependency graph small.
Self-Improvement
If you encounter a pattern that doesn't work with newer TypeScript versions, a missing pattern, or incorrect guidance in this skill, don't just work around it — fix the skill:
- Create a PR from a fresh worktree of
https://github.com/quatico-solutions/agent-skillson a new branch, fixing the issue directly - Or file an issue on
https://github.com/quatico-solutions/agent-skillswith: what failed, the actual behavior, and the suggested fix
Never silently work around a skill gap. The fix benefits all future sessions.
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.

