Skip to content
Back to skills

Typescript Best Practices

ASecurity

TypeScript conventions for precise contracts and valid program states. Use when writing or reviewing TypeScript types, state models, optional values, or untrusted boundaries.

  • 14 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added September 6, 2026
developmenttypescriptrustexpressgitapi

Works with

  • api

Security analysis

A100/100

Pro scans all 20 files and shows the line behind each finding

Scanned November 15, 2026

npx -y skills add Casper-Studios/casper-marketplace --skill typescript-best-practices --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Typescript Best Practices?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Typescript Best Practices
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/casper-studios-typescript-best-practices/badge)](https://www.skillsdirectory.com/skills/casper-studios-typescript-best-practices)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: typescript-best-practices
description: TypeScript conventions for precise contracts and valid program states. Use when writing or reviewing TypeScript types, state models, optional values, or untrusted boundaries.
license: MPL-2.0
metadata:
  author: 'Basti Ortiz <ortiz@bastidood.dev>'
  source: 'https://github.com/BastiDood/skills'
---

# TypeScript Best Practices

> [!IMPORTANT]
> Apply the `codebase-architecture:code-style` principles through TypeScript's type system: treat types as precise, compiler-checked models of valid program states and boundaries, preserve inference where it carries information, and narrow uncertainty explicitly so invalid states and unhandled cases cannot silently reach runtime.

## References

Read the references that apply to the current task before writing or reviewing TypeScript.

1. Preserve TypeScript's specific inference whenever it proves the intended contract.
   - [Infer derivable types and impose annotations only where they prove a required contract](./references/inference-over-annotation.md), keeping inferred locals and filter narrowing while the defining module owns named result contracts.
   - [Declare the intended state set at a literal inference boundary](./references/literal-union-inference.md) when an API infers a closed state from one initial literal so later valid members remain assignable.
   - [Model named durable closed sets of serialized primitive values as `const enum`s](./references/const-enums.md) rather than string-literal unions.
   - [Preserve readonly tuple or literal-union inference with `as const`](./references/literal-const-assertions.md) when consumers require it rather than treating the assertion as a default.
   - [Use structural conformance](./references/structural-conformance.md) when a value must satisfy a shape without losing its precise inferred literals instead of widening it with an annotation.
2. Model domain contracts and closed states precisely.
   - [Choose `interface` by default and compose it with `extends`](./references/interfaces-and-types.md) for handwritten object contracts; use a `Pick` alias for an exact subset of an existing contract.
   - [Make decisions over closed enums or unions exhaustive](./references/exhaustive-decisions.md) so new members cannot silently reach a fallback.
3. Narrow uncertainty and absence explicitly at the boundary that owns it.
   - [Contain type escape hatches](./references/escape-hatches.md) by narrowing or validating `any` and `unknown` inside the boundary that receives them so neither can escape to callers or domain state.
   - [Replace unchecked assertions with runtime proof](./references/unchecked-assertions.md) at a serialization boundary when a value is untrusted or insufficiently typed.
   - [Establish an absent-value invariant explicitly](./references/non-null-assertions.md) when an operation requires it rather than asserting it with `!`.
   - [Preserve omission in optional arguments and object fields](./references/optional-arguments.md); omit absent arguments, conditionally insert present fields, and write `void 0` only where an `undefined` value must be present.
   - [Narrow optional values with `typeof` guards before required calls](./references/optional-value-narrowing.md), or preserve absence in the return or state contract when it is expected.
   - [Do not fabricate a default for missing required input](./references/no-fabricated-defaults.md) unless the domain contract assigns that exact omitted-value meaning.
   - [Narrow indexed access and destructured bindings](./references/indexed-access-and-destructuring.md) before required use when `noUncheckedIndexedAccess` preserves possible absence.
   - [Validate a required regular-expression capture first](./references/regex-capture-groups.md) because named and positional groups can be absent.

Files in this skill

  • LICENSE.txt16.3 KB
  • SKILL.md3.9 KB
  • agents/openai.yaml144 B
  • references/array-index-guards.md604 B
  • references/const-enums.md778 B
  • references/discriminated-union-state.md970 B
  • references/escape-hatches.md865 B
  • references/exhaustive-decisions.md1.2 KB
  • references/inference-over-annotation.md2.9 KB
  • references/interfaces-and-types.md1 KB
  • references/literal-const-assertions.md459 B
  • references/literal-union-inference.md1.3 KB
  • references/no-fabricated-defaults.md454 B
  • references/non-null-assertions.md584 B
  • references/optional-arguments.md376 B
  • references/optional-value-narrowing.md339 B
  • references/regex-capture-groups.md580 B
  • references/split-results.md509 B
  • references/structural-conformance.md775 B
  • references/unchecked-assertions.md597 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.