Back to list
15195999826

using-typescript-types

by 15195999826

0🍴 0📅 Jan 13, 2026

SKILL.md


name: using-typescript-types description: >- Guides TypeScript type usage: type vs interface decision, avoiding any, and Zod runtime validation. Use when defining types, choosing type patterns, or validating external data. Triggers: "type vs interface", "any", "unknown", "Zod", "运行时验证", "类型".

TypeScript 类型规范

核心约定

规范项目选择
默认类型定义type(不用 interface
未知类型unknown(禁止 any
字符串常量联合类型(不用 enum
外部数据Zod 验证(不用 as Type

Type vs Interface

默认用 type
│
├─ 需要声明合并(扩展第三方库)→ interface
├─ 类的契约(implements)→ interface
└─ 其他情况 → type

禁止 any

// ✗ any
function process(data: any) { }

// ✓ unknown + 类型守卫
function process(data: unknown) {
  if (typeof data === 'string') {
    console.log(data.toUpperCase())
  }
}

// 类型谓词
function isUser(v: unknown): v is User {
  return typeof v === 'object' && v !== null && 'id' in v
}

运行时验证 (Zod)

外部数据必须验证(API、表单、环境变量、配置文件):

import { z } from 'zod'

const UserSchema = z.object({
  id: z.string(),
  name: z.string(),
  email: z.string().email(),
})

type User = z.infer<typeof UserSchema>

// 验证 API 响应
const user = UserSchema.parse(await res.json())

详细 Zod 模式参见 references/zod-patterns.md

Score

Total Score

50/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

0/10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

Reviews

💬

Reviews coming soon