Install
openclaw skills install fec-typescript-type-safetyUse when designing, implementing, or reviewing TypeScript type contracts, advanced generics, discriminated unions, type guards, API DTOs, component props, public utility types, or type-level regressions in frontend projects. Prefer code review for broad PR review; Chinese triggers include TypeScript 类型安全, 类型建模, 泛型, 判别联合, 类型收窄.
openclaw skills install fec-typescript-type-safety为前端代码建立可演进的类型契约,减少 any、断言和运行时形状漂移。
把类型分为外部输入、领域模型、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",
};
}
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";
}
异步状态、权限分支、支付状态等有限状态,用判别字段让新增分支在编译期暴露。
type Loadable<T> =
| { 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<{ 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);
}
}
泛型应减少重复并保持调用方推断清晰。复杂类型超过阅读成本时,优先拆具名类型。
interface SelectOption<TValue extends string | number> {
label: string;
value: TValue;
}
interface SelectProps<TValue extends string | number> {
value: TValue;
options: Array<SelectOption<TValue>>;
onChange: (value: TValue) => void;
}
export function Select<TValue extends string | number>({
value,
options,
onChange,
}: SelectProps<TValue>) {
return (
<select value={value} onChange={(event) => onChange(event.target.value as TValue)}>
{options.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
);
}
公共工具类型、组件库类型和 SDK 类型,应有轻量类型测试或编译期断言。
type Equal<TLeft, TRight> =
(<T>() => T extends TLeft ? 1 : 2) extends <T>() => T extends TRight ? 1 : 2
? true
: false;
type Expect<T extends true> = T;
type UserStatus = UserViewModel["status"];
type _UserStatusTest = Expect<Equal<UserStatus, "active" | "disabled">>;
涉及高级泛型约束、映射类型、模板字面量类型、类型测试、DTO 映射或类型审查清单时,加载 references/type-safety-patterns.md。
any、无守卫的非空断言和不相关类型之间的强制 as。产出清晰的类型边界、可收窄的数据模型、必要的类型测试和验证命令。评审时应列出类型风险、运行时影响、推荐建模方式和是否需要专项修复。