mcpField
T
typescript

typescript

github.com/prowler-cloud开发15.2k254 次安装今天更新
代码仓库

关于 typescript

typescript 是面向 TypeScript 开发者的编码技能,介绍常量、接口、未知值、工具类型、类型守卫及关联可选属性的类型安全模式。

主要功能

  1. 01用 const 对象生成类型
  2. 02以独立接口表示嵌套数据
  3. 03用 unknown 替代 any
  4. 04TypeScript 工具类型
  5. 05类型守卫与判别联合

适用场景

  1. 01定义可复用的状态类型
  2. 02描述嵌套用户数据
  3. 03避免无效的属性组合

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/prowler-cloud/prowler --skill typescript

安装后在 Claude Code 中输入/typescript确认已加载。

详细介绍

Const Types 模式(必需)

// ✅ ALWAYS: Create const object first, then extract type
const STATUS = {
  ACTIVE: "active",
  INACTIVE: "inactive",
  PENDING: "pending",
} as const;

type Status = (typeof STATUS)[keyof typeof STATUS];

// ❌ NEVER: Direct union types
type Status = "active" | "inactive" | "pending";

为什么? 单一事实来源、运行时值、自动补全,以及更轻松的重构。

扁平接口(必需)

// ✅ ALWAYS: One level depth, nested objects → dedicated interface
interface UserAddress {
  street: string;
  city: string;
}

interface User {
  id: string;
  name: string;
  address: UserAddress;  // Reference, not inline
}

interface Admin extends User {
  permissions: string[];
}

// ❌ NEVER: Inline nested objects
interface User {
  address: { street: string; city: string };  // NO!
}

切勿使用 any

// ✅ Use unknown for truly unknown types
function parse(input: unknown): User {
  if (isUser(input)) return input;
  throw new Error("Invalid input");
}

// ✅ Use generics for flexible types
function first<T>(arr: T[]): T | undefined {
  return arr[0];
}

// ❌ NEVER
function parse(input: any): any { }

工具类型

Pick<User, "id" | "name">     // Select fields
Omit<User, "id">              // Exclude fields
Partial<User>                 // All optional
Required<User>                // All required
Readonly<User>                // All readonly
Record<string, User>          // Object type
Extract<Union, "a" | "b">     // Extract from union
Exclude<Union, "a">           // Exclude from union
NonNullable<T | null>         // Remove null/undefined
ReturnType<typeof fn>         // Function return type
Parameters<typeof fn>         // Function params tuple

类型守卫

function isUser(value: unknown): value is User {
  return (
    typeof value === "object" &&
    value !== null &&
    "id" in value &&
    "name" in value
  );
}

相互关联的可选属性(必需)

不要将语义上相互关联的属性建模为独立的可选属性——这会允许无效的半成品状态通过编译,却在运行时出错。使用带有 never 的判别联合,使无效组合无法成立。

// ❌ BEFORE: Independent optionals — half-states allowed
interface PaginationProps {
  onPageChange?: (page: number) => void;
  pageSize?: number;
  currentPage?: number;
}

// ✅ AFTER: Discriminated union — shape is all-or-nothing
type ControlledPagination = {
  controlled: true;
  currentPage: number;
  pageSize: number;
  onPageChange: (page: number) => void;
};

type UncontrolledPagination = {
  controlled: false;
  currentPage?: never;
  pageSize?: never;
  onPageChange?: never;
};

type PaginationProps = ControlledPagination | UncontrolledPagination;

关键规则: 如果两个或更多属性只有在一起时才有意义,它们就应属于同一个判别联合分支。将它们混合为独立的可选属性,会把正确性责任从类型系统转移到运行时守卫。

导入类型

import type { User } from "./types";
import { createUser, type Config } from "./utils";

评论

支持 Markdown