agentleFS
theonly1metheonly1me/shadowclone/.agents/skills/typescript-type-safety/SKILL.md

typescript-type-safety

Skill25 starsChanged 32 days agoMITView on GitHub

Use when changing TypeScript types, parsing input that enters typed code, or removing unsafe casts. Also use it when the user says "fix the types", "remove the as casts", or "make invalid states impossible". Models each valid state with types, so that invalid combinations fail to compile. Checks outside input with a type guard or a schema instead of an assertion. Not for designing module boundaries (use `design-deep-modules`).

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

8 rules. About 0 are common, 8 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈8

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Keep the relation between keys and values with generics, mapped types, or a typed setter.

    line 26
  2. Distinctive:

    Run the type check, and run the tests for the boundary checks.

    line 30
  3. Distinctive:

    Easy route: Keep the cast, because the file usually has the right shape.

    line 35
  4. Distinctive:

    Keep one source of truth for each discriminator.

    line 42
  5. Distinctive:

    Keep literal types until a wider type is necessary.

    line 43
  6. Distinctive:

    Put an imprecise library type in one adapter that returns a precise project type.

    line 44
  7. Distinctive:

    Use a narrow assertion only for a fact that a runtime check cannot express, and write down why.

    line 45
  8. Distinctive:

    Follow the stricter rules of the repository where they apply.

    line 46

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Represent State in TypeScript Types

Use when

Values change together, optional fields hide different states, outside input enters typed code, or the compiler asks for an assertion. Each of these signals a missing type or a missing check.

Gates

  1. Does each valid state have its own type, such as a member of a discriminated union?
  2. Does outside input enter as unknown, and pass a type guard or a schema before use?
  3. Is the code free of any, non-null assertions, and casts, except as const?
  4. Does each switch over a union handle each member, so that a new member fails to compile?
  5. Does a failure that callers must handle appear in the return type?

Process

  1. Name the valid states and the moves between them. Use a discriminated union when only some field combinations are valid.
  2. Keep the relation between keys and values with generics, mapped types, or a typed setter.
  3. Accept outside input as unknown. Check it at the boundary with a schema or a type guard.
  4. Narrow values with control flow, type guards, or destructuring. Make a valid absence explicit.
  5. Return a type that makes callers handle each meaningful failure.
  6. Run the type check, and run the tests for the boundary checks.

Example

Situation: A function reads a config file and casts the parsed JSON to Config. Easy route: Keep the cast, because the file usually has the right shape. Hidden cost: A config file with one wrong field passes the cast. The program fails later, far from the cause. Best route: Parse the JSON with a schema at the boundary. Return a clear error that names the field. Evidence: A test with a wrong field type now fails at the boundary with "sources.git-metadata: expected boolean". The type check passed with no cast left.

Guardrails

  • Keep one source of truth for each discriminator.
  • Keep literal types until a wider type is necessary.
  • Put an imprecise library type in one adapter that returns a precise project type.
  • Use a narrow assertion only for a fact that a runtime check cannot express, and write down why.
  • Follow the stricter rules of the repository where they apply.

Completion

  • The type that now rejects the invalid state.
  • One example that the compiler now rejects, with its error line.
  • The type check command and the boundary test command, with their result lines.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.