Skip to main content

typescript-patterns

TypeScript best practices and patterns. Use when writing TypeScript, fixing type errors, or working with generics.

来源信息

仓库
Tibsfox/gsd-skill-creator
最近来源活动
2026年4月25日 21:14
检测到的 SKILL.md 语言
英语
星标
70
分支
10

安装方式

默认使用会先检查来源的 Prompt;你也可以切换为直接命令,或下载本地副本。

检查来源文件

决定是否安装前,请先阅读 SKILL.md,以及 SkillsMP 当前展示的配套文件。

正在显示 SKILL.md

SKILL.md
来源说明 · 只读预览
name
typescript-patterns
description
TypeScript best practices and patterns. Use when writing TypeScript, fixing type errors, or working with generics.
version
1.0.0
format
2025-10-02T00:00:00.000Z
triggers
["writing TypeScript, fixing type errors, or working with generics"]
updated
2026-04-25T00:00:00.000Z
status
ACTIVE
# TypeScript Patterns ## Type vs Interface | Use Case | Prefer | |----------|--------| | Object shape | interface | | Union types | type | | Extending shapes | interface | | Intersection | type | | Tuple/primitives | type | ## Key Patterns **Discriminated Unions** — model state with tagged types: ```typescript type Result<T> = { ok: true; value: T } | { ok: false; error: Error }; ``` **Type Guards** — narrow at runtime: ```typescript function isUser(obj: unknown): obj is User { return typeof obj === 'object' && obj !== null && 'id' in obj; } ``` **Generic Constraints:** ```typescript function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] { return obj[key]; } ``` ## Utility Types | Type | Purpose | |------|---------| | Partial\<T\> | All optional | | Required\<T\> | All required | | Pick\<T,K\> | Select properties | | Omit\<T,K\> | Remove properties | | Record\<K,V\> | Key-value map | | ReturnType\<T\> | Function return type | ## Avoid - `any` — use `unknown` and narrow - Type assertions (`as`) — use type guards - `!` operator — handle null properly - Missing return types on complex functions
在 GitHub 查看