mcpField
T
typescript

typescript

github.com/alinaqi开发710312 次安装2 周前更新
代码仓库

关于 typescript

typescript 是一项提供 TypeScript 项目规范与示例的技能,涵盖严格类型、项目结构、工具、测试和 CI 质量检查,适用于建立或维护一致开发实践。

主要功能

  1. 01严格 TypeScript 配置
  2. 02项目结构建议
  3. 03ESLint 和 Prettier 工具配置
  4. 04Jest 测试示例
  5. 05类型安全模式与校验
  6. 06提交前及 CI 检查

适用场景

  1. 01配置 TypeScript 项目的质量检查
  2. 02为应用服务编写测试
  3. 03选择安全的结果类型和 ID 类型模式
  4. 04检查暂存的更改

安装

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

$npx skills add https://github.com/alinaqi/maggy --skill typescript

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

详细介绍

TypeScript 技能


严格模式(不可妥协)

// tsconfig.json
{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

项目结构

project/
├── src/
│   ├── core/               # Pure business logic
│   │   ├── types.ts        # Domain types/interfaces
│   │   ├── services/       # Pure functions
│   │   └── index.ts        # Public API
│   ├── infra/              # Side effects
│   │   ├── api/            # HTTP handlers
│   │   ├── db/             # Database operations
│   │   └── external/       # Third-party integrations
│   └── utils/              # Shared utilities
├── tests/
│   ├── unit/
│   └── integration/
├── package.json
├── tsconfig.json
└── CLAUDE.md

工具链(必需)

// package.json scripts
{
  "scripts": {
    "lint": "eslint src/ --ext .ts,.tsx",
    "typecheck": "tsc --noEmit",
    "test": "jest",
    "test:coverage": "jest --coverage",
    "format": "prettier --write 'src/**/*.ts'"
  }
}
// eslint.config.js
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config(
  eslint.configs.recommended,
  ...tseslint.configs.strictTypeChecked,
  {
    rules: {
      '@typescript-eslint/no-explicit-any': 'error',
      '@typescript-eslint/explicit-function-return-type': 'error',
      'max-lines-per-function': ['error', 20],
      'max-depth': ['error', 2],
      'max-params': ['error', 3],
    }
  }
);

使用 Jest 进行测试

// tests/unit/services/user.test.ts
import { calculateTotal } from '../../../src/core/services/pricing';

describe('calculateTotal', () => {
  it('returns sum of item prices', () => {
    // Arrange
    const items = [{ price: 10 }, { price: 20 }];

    // Act
    const result = calculateTotal(items);

    // Assert
    expect(result).toBe(30);
  });

  it('returns zero for empty array', () => {
    expect(calculateTotal([])).toBe(0);
  });

  it('throws on invalid item', () => {
    expect(() => calculateTotal([{ invalid: 'item' }])).toThrow();
  });
});

GitHub Actions

name: TypeScript Quality Gate

on: [push, pull_request]

jobs:
  quality:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          
      - name: Install dependencies
        run: npm ci
        
      - name: Lint
        run: npm run lint
        
      - name: Type Check
        run: npm run typecheck
        
      - name: Test with Coverage
        run: npm run test:coverage
        
      - name: Coverage Threshold (80%)
        run: npm run test:coverage -- --coverageThreshold='{"global":{"branches":80,"functions":80,"lines":80,"statements":80}}'

提交前钩子

使用 Husky + lint-staged:

npm install -D husky lint-staged
npx husky init
// package.json
{
  "lint-staged": {
    "*.{ts,tsx}": [
      "eslint --fix",
      "prettier --write"
    ]
  }
}
# .husky/pre-commit
npx lint-staged
npx tsc --noEmit
npm run test -- --onlyChanged --passWithNoTests

每次提交时都会运行:

  1. 对暂存文件运行 ESLint + Prettier
  2. 对整个项目进行类型检查
  3. 仅测试有更改的文件

类型模式

用于结果的可辨识联合类型
type Result<T> =
  | { ok: true; value: T }
  | { ok: false; error: string };

function parseUser(data: unknown): Result<User> {
  // Type-safe error handling without exceptions
}
用于 ID 的品牌类型
type UserId = string & { readonly brand: unique symbol };
type OrderId = string & { readonly brand: unique symbol };

// Can't accidentally pass UserId where OrderId expected
function getOrder(orderId: OrderId): Order { ... }
字面量的 const 断言
const STATUSES = ['pending', 'active', 'closed'] as const;
type Status = typeof STATUSES[number]; // 'pending' | 'active' | 'closed'
使用 Zod 进行运行时验证
import { z } from 'zod';

const UserSchema = z.object({
  email: z.string().email(),
  name: z.string().min(1).max(100),
});

type User = z.infer<typeof UserSchema>;

TypeScript 反模式

  • ❌ any 类型 - 使用 unknown 并进行收窄
  • ❌ 类型断言(as)- 使用类型守卫
  • ❌ 非空断言(!)- 明确处理 null
  • ❌ 不加说明地使用 @ts-ignore
  • ❌ 枚举 - 使用 const 对象或联合类型
  • ❌ 用类表示数据 - 使用接口/类型
  • ❌ 默认导出 - 使用命名导出

评论

支持 Markdown