SkilVaultSign inStart free

Catalogue / strict-typescript-value-modeling

strict-typescript-value-modeling

Older skill: source not recordedSignedApplies strict TypeScript modeling: const-map value types, flat interfaces, generics, unknown-checked boundaries, utility types, and type-only imports. Use when the user asks to "remove any", "type this API response", "tighten types", "add a type guard", or refactors .ts/.tsx code under strict mode.…
---
name: strict-typescript-value-modeling
description: >-
  Applies strict TypeScript modeling: const-map value types, flat interfaces,
  generics, unknown-checked boundaries, utility types, and type-only imports.
  Use when the user asks to "remove any", "type this API response", "tighten
  types", "add a type guard", or refactors .ts/.tsx code under strict mode.
  Not for runtime validation libraries, JavaScript files, or choosing a build
  tool.
---

Make the runtime values and declared model agree

Inspect the compiler configuration and existing code before proposing changes. Describe affected files and behavior and obtain explicit human confirmation before editing, installing, generating artifacts, or making remote requests. Preserve external contracts while tightening internal models.

For named finite values that also appear at runtime, define a constant map first and derive the type from it. Use this skill's convention rather than maintaining an unrelated hand-written literal union:

const SHIPPING = { ROAD: "road", SEA: "sea", AIR: "air" } as const;
type Shipping = (typeof SHIPPING)[keyof typeof SHIPPING];

interface Destination { port: string; country: string }
interface Consignment { code: string; destination: Destination; mode: Shipping }
interface InsuredConsignment extends Consignment { insuredValue: number }

Keep interfaces flat: nested objects receive their own named interface and are referenced as fields. Extend interfaces for a meaningful shared model. This makes runtime constants, autocomplete, and refactoring work from the same value set.

Narrow at the boundary and preserve relationships

Use unknown for an unverified value and check it before returning a domain type. Never use any to avoid explaining the boundary. A guard must check property types and allowed values, not merely whether names exist:

function isDestination(value: unknown): value is Destination {
  if (typeof value !== "object" || value === null) return false;
  return "port" in value && typeof value.port === "string"
    && "country" in value && typeof value.country === "string";
}
function requireDestination(value: unknown): Destination {
  if (!isDestination(value)) throw new Error("Invalid destination");
  return value;
}
function last<T>(items: readonly T[]): T | undefined {
  return items.length ? items[items.length - 1] : undefined;
}

Generics retain the connection between inputs and outputs. Include the empty-list case rather than claiming every collection access yields a value.

Transform existing models instead of duplicating them

| Utility | What it does | |---|---| | Pick<User, "id" \| "name"> | Keep only selected fields | | Omit<User, "id"> | Drop named fields | | Partial<User> | Every field optional | | Required<User> | Every field required | | Readonly<User> | Every field readonly (compile-time only) | | Record<string, User> | Map key type to value type | | Extract<Union, "a" \| "b"> | Keep union members matching a type | | Exclude<Union, "a"> | Remove union members matching a type | | NonNullable<T \| null> | Strip null and undefined | | ReturnType<typeof fn> | Derive a function's result type | | Parameters<typeof fn> | Derive a function's argument tuple |

Remember: these reshape static types only. They add no validation, defaults, or runtime immutability.

Use import type for names consumed purely as types. In a mixed import, mark the type-only names with the inline type modifier:

import type { Consignment } from "./models";
import { bookShipment, type Port } from "./logistics";

Resolve imports from real project modules — never assume a bundled helper exists.

After approved edits, run the project's installed type checker and the relevant behavior tests. Exercise malformed values and missing properties, not just valid inputs, and report compile-time and runtime evidence separately.

Never

  • Introduce any, cast unknown data to a trusted model without checks, or weaken strictness to hide errors.
  • Maintain duplicate literal value lists when a runtime constant map is the chosen convention.
  • Write nested anonymous model objects where a dedicated interface is required here.
  • Edit files or install a compiler without explicit confirmation.