← Back to Skills Marketplace
bovinphang

Typescript Type Safety

by Bovin Phang · GitHub ↗ · v2.5.0 · MIT-0
cross-platform ✓ Security Clean
29
Downloads
0
Stars
0
Active Installs
1
Versions
Install in OpenClaw
/install fec-typescript-type-safety
Description
Use when designing, implementing, or reviewing TypeScript type contracts, advanced generics, discriminated unions, type guards, API DTOs, component props, pu...
README (SKILL.md)

TypeScript 类型安全

Purpose

为前端代码建立可演进的类型契约,减少 any、断言和运行时形状漂移。

Procedure

1. 先确定类型边界

把类型分为外部输入、领域模型、UI view model、组件 props、工具函数 API。不要让后端 DTO、表单模型和 UI 展示模型互相冒充。

interface UserDto {
  id: string;
  display_name: string;
  status: "ACTIVE" | "DISABLED";
}

interface UserViewModel {
  id: string;
  displayName: string;
  status: "active" | "disabled";
}

export function mapUserDto(dto: UserDto): UserViewModel {
  return {
    id: dto.id,
    displayName: dto.display_name,
    status: dto.status === "ACTIVE" ? "active" : "disabled",
  };
}

2. 用 unknown 和收窄处理不可信数据

外部输入先校验再使用。不要用 as 让编译器闭嘴。

interface ApiErrorBody {
  message: string;
}

function isApiErrorBody(value: unknown): value is ApiErrorBody {
  return (
    typeof value === "object" &&
    value !== null &&
    "message" in value &&
    typeof value.message === "string"
  );
}

export function getApiErrorMessage(value: unknown): string {
  return isApiErrorBody(value) ? value.message : "Unexpected error";
}

3. 用判别联合表达状态机

异步状态、权限分支、支付状态等有限状态,用判别字段让新增分支在编译期暴露。

type Loadable\x3CT> =
  | { state: "idle" }
  | { state: "loading" }
  | { state: "success"; data: T }
  | { state: "error"; error: Error };

function assertNever(value: never): never {
  throw new Error(`Unhandled state: ${JSON.stringify(value)}`);
}

export function renderUserState(user: Loadable\x3C{ name: string }>): string {
  switch (user.state) {
    case "idle":
      return "Ready";
    case "loading":
      return "Loading";
    case "success":
      return user.data.name;
    case "error":
      return user.error.message;
    default:
      return assertNever(user);
  }
}

4. 让泛型服务调用方,而不是炫技

泛型应减少重复并保持调用方推断清晰。复杂类型超过阅读成本时,优先拆具名类型。

interface SelectOption\x3CTValue extends string | number> {
  label: string;
  value: TValue;
}

interface SelectProps\x3CTValue extends string | number> {
  value: TValue;
  options: Array\x3CSelectOption\x3CTValue>>;
  onChange: (value: TValue) => void;
}

export function Select\x3CTValue extends string | number>({
  value,
  options,
  onChange,
}: SelectProps\x3CTValue>) {
  return (
    \x3Cselect value={value} onChange={(event) => onChange(event.target.value as TValue)}>
      {options.map((option) => (
        \x3Coption key={option.value} value={option.value}>
          {option.label}
        \x3C/option>
      ))}
    \x3C/select>
  );
}

5. 用类型测试保护公共类型

公共工具类型、组件库类型和 SDK 类型,应有轻量类型测试或编译期断言。

type Equal\x3CTLeft, TRight> =
  (\x3CT>() => T extends TLeft ? 1 : 2) extends \x3CT>() => T extends TRight ? 1 : 2
    ? true
    : false;

type Expect\x3CT extends true> = T;

type UserStatus = UserViewModel["status"];
type _UserStatusTest = Expect\x3CEqual\x3CUserStatus, "active" | "disabled">>;

详细参考

涉及高级泛型约束、映射类型、模板字面量类型、类型测试、DTO 映射或类型审查清单时,加载 references/type-safety-patterns.md

Constraints

  • 避免 any、无守卫的非空断言和不相关类型之间的强制 as
  • 不把大型匿名类型堆在函数参数或 JSX props 上;提取具名类型表达业务含义。
  • 不为简单业务过度设计递归条件类型;类型复杂度应服务可维护性。
  • 公共 API 的输入输出必须显式标注,局部变量可依赖推断。
  • 类型建模不能替代运行时校验;外部数据仍需 schema 或 type guard。

Expected Output

产出清晰的类型边界、可收窄的数据模型、必要的类型测试和验证命令。评审时应列出类型风险、运行时影响、推荐建模方式和是否需要专项修复。

Usage Guidance
Installers should expect a passive review-quality skill that helps an agent reason about TypeScript contracts. It may lead the agent to suggest or make type-related code changes when the user asks for implementation or review, but the skill itself does not add executable behavior or special access.
Capability Assessment
Purpose & Capability
The stated purpose is TypeScript type modeling and review guidance, and the artifacts consistently provide examples for DTO mapping, type guards, discriminated unions, generics, and type-level tests.
Instruction Scope
Runtime instructions are scoped to frontend TypeScript design or review tasks and reference a local markdown file for deeper patterns; there are no prompt overrides, hidden role changes, or unrelated instructions.
Install Mechanism
The package declares no dependencies or install scripts and contains only markdown and JSON files under the standard SKILL.md layout.
Credentials
The skill does not request network access, credentials, broad filesystem reads, local indexing, or external tool execution; its code blocks are illustrative TypeScript examples aligned with the purpose.
Persistence & Privilege
No background workers, persistence mechanisms, privilege escalation, automatic mutation, or destructive commands were present in the artifacts.
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install fec-typescript-type-safety
  3. After installation, invoke the skill by name or use /fec-typescript-type-safety
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
v2.5.0
- Expanded SKILL.md with detailed guidance for designing and reviewing TypeScript type contracts in frontend projects. - Added best practices on type boundaries, using unknown and type narrowing, discriminated unions, and generics. - Included examples for DTO mapping, API error handling, state modeling, component props, and type-level tests. - Listed actionable constraints to avoid misuse of TypeScript features and clarify modeling priorities. - Clarified expected outputs for code review and risk identification.
Metadata
Slug fec-typescript-type-safety
Version 2.5.0
License MIT-0
All-time Installs 0
Active Installs 0
Total Versions 1
Frequently Asked Questions

What is Typescript Type Safety?

Use when designing, implementing, or reviewing TypeScript type contracts, advanced generics, discriminated unions, type guards, API DTOs, component props, pu... It is an AI Agent Skill for Claude Code / OpenClaw, with 29 downloads so far.

How do I install Typescript Type Safety?

Run "/install fec-typescript-type-safety" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.

Is Typescript Type Safety free?

Yes, Typescript Type Safety is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does Typescript Type Safety support?

Typescript Type Safety is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created Typescript Type Safety?

It is built and maintained by Bovin Phang (@bovinphang); the current version is v2.5.0.

💬 Comments