← 返回 Skills 市场
bovinphang

Typescript Type Safety

作者 Bovin Phang · GitHub ↗ · v2.5.0 · MIT-0
cross-platform ✓ 安全检测通过
29
总下载
0
收藏
0
当前安装
1
版本数
在 OpenClaw 中安装
/install fec-typescript-type-safety
功能描述
Use when designing, implementing, or reviewing TypeScript type contracts, advanced generics, discriminated unions, type guards, API DTOs, component props, pu...
使用说明 (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

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

安全使用建议
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.
能力评估
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.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install fec-typescript-type-safety
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /fec-typescript-type-safety 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
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.
元数据
Slug fec-typescript-type-safety
版本 2.5.0
许可证 MIT-0
累计安装 0
当前安装数 0
历史版本数 1
常见问题

Typescript Type Safety 是什么?

Use when designing, implementing, or reviewing TypeScript type contracts, advanced generics, discriminated unions, type guards, API DTOs, component props, pu... 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 29 次。

如何安装 Typescript Type Safety?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install fec-typescript-type-safety」即可一键安装,无需额外配置。

Typescript Type Safety 是免费的吗?

是的,Typescript Type Safety 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

Typescript Type Safety 支持哪些平台?

Typescript Type Safety 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Typescript Type Safety?

由 Bovin Phang(@bovinphang)开发并维护,当前版本 v2.5.0。

💬 留言讨论