mastering-typescript by spillwavesolutions/mastering-typescript-skill
npx skills add https://github.com/spillwavesolutions/mastering-typescript-skill --skill mastering-typescript使用 TypeScript 5.9+ 构建企业级、类型安全的应用程序。
兼容性: TypeScript 5.9+、Node.js 22 LTS、Vite 7、NestJS 11、React 19
# 使用 ESM 初始化 TypeScript 项目
pnpm create vite@latest my-app --template vanilla-ts
cd my-app && pnpm install
# 配置严格的 TypeScript
cat > tsconfig.json << 'EOF'
{
"compilerOptions": {
"target": "ES2024",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"esModuleInterop": true,
"skipLibCheck": true
}
}
EOF
在以下情况下使用:
在开始任何 TypeScript 项目之前:
- [ ] 使用 pnpm 进行包管理(更快、磁盘效率更高)
- [ ] 配置 ESM 优先(在 package.json 中设置 type: "module")
- [ ] 在 tsconfig.json 中启用严格模式
- [ ] 使用 @typescript-eslint 设置 ESLint
- [ ] 添加 Prettier 以保持一致的格式
- [ ] 配置 Vitest 进行测试
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
const name: string = "Alice";
const age: number = 30;
const active: boolean = true;
const id: bigint = 9007199254740991n;
const key: symbol = Symbol("unique");
// 联合:值可以是几种类型之一
type Status = "pending" | "approved" | "rejected";
// 交叉:值必须满足所有类型
type Employee = Person & { employeeId: string };
// 可辨识联合,用于类型安全处理
type Result<T> =
| { success: true; data: T }
| { success: false; error: string };
function handleResult<T>(result: Result<T>): T | null {
if (result.success) {
return result.data; // TypeScript 知道此处 data 存在
}
console.error(result.error);
return null;
}
// typeof 守卫
function process(value: string | number): string {
if (typeof value === "string") {
return value.toUpperCase();
}
return value.toFixed(2);
}
// 自定义类型守卫
interface User { type: "user"; name: string }
interface Admin { type: "admin"; permissions: string[] }
function isAdmin(person: User | Admin): person is Admin {
return person.type === "admin";
}
satisfies 运算符 (TS 5.0+)验证类型一致性,同时保留类型推断:
// 问题:类型断言丢失了具体的类型信息
const colors1 = {
red: "#ff0000",
green: "#00ff00"
} as Record<string, string>;
colors1.red.toUpperCase(); // OK,但 red 可能为 undefined
// 解决方案:satisfies 保留字面量类型
const colors2 = {
red: "#ff0000",
green: "#00ff00"
} satisfies Record<string, string>;
colors2.red.toUpperCase(); // OK,并且 TypeScript 知道 red 存在
function first<T>(items: T[]): T | undefined {
return items[0];
}
const num = first([1, 2, 3]); // number | undefined
const str = first(["a", "b"]); // string | undefined
interface HasLength {
length: number;
}
function logLength<T extends HasLength>(item: T): T {
console.log(item.length);
return item;
}
logLength("hello"); // OK: string 有 length 属性
logLength([1, 2, 3]); // OK: 数组有 length 属性
logLength(42); // Error: number 没有 length 属性
interface ApiResponse<T> {
data: T;
status: number;
timestamp: Date;
}
async function fetchUser(id: string): Promise<ApiResponse<User>> {
const response = await fetch(`/api/users/${id}`);
const data = await response.json();
return {
data,
status: response.status,
timestamp: new Date()
};
}
| 类型 | 用途 | 示例 |
|---|---|---|
Partial<T> | 所有属性变为可选 | Partial<User> |
Required<T> | 所有属性变为必需 | Required<Config> |
Pick<T, K> | 选择特定属性 | `Pick<User, "id" |
Omit<T, K> | 排除特定属性 | Omit<User, "password"> |
Record<K, V> | 具有类型化键/值的对象 | Record<string, number> |
ReturnType<F> | 提取函数返回类型 | ReturnType<typeof fn> |
Parameters<F> | 提取函数参数 | Parameters<typeof fn> |
Awaited<T> | 解包 Promise 类型 | Awaited<Promise<User>> |
// 基础条件类型
type IsString<T> = T extends string ? true : false;
// 提取数组元素类型
type ArrayElement<T> = T extends (infer E)[] ? E : never;
type Numbers = ArrayElement<number[]>; // number
type Strings = ArrayElement<string[]>; // string
// 实用:提取 Promise 结果类型
type UnwrapPromise<T> = T extends Promise<infer R> ? R : T;
// 使所有属性变为只读
type Immutable<T> = {
readonly [K in keyof T]: T[K];
};
// 使所有属性可为空
type Nullable<T> = {
[K in keyof T]: T[K] | null;
};
// 为每个属性创建 getter 函数
type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
interface Person { name: string; age: number }
type PersonGetters = Getters<Person>;
// { getName: () => string; getAge: () => number }
// 类型化的函数组件
interface ButtonProps {
label: string;
onClick: () => void;
variant?: "primary" | "secondary";
}
const Button: React.FC<ButtonProps> = ({ label, onClick, variant = "primary" }) => (
<button className={variant} onClick={onClick}>
{label}
</button>
);
// 类型化的钩子
const [count, setCount] = useState<number>(0);
const userRef = useRef<HTMLInputElement>(null);
// 使用 class-validator 的类型安全 DTO
import { IsString, IsEmail, MinLength } from 'class-validator';
class CreateUserDto {
@IsString()
@MinLength(2)
name: string;
@IsEmail()
email: string;
}
// 或使用 Zod(现代方法)
import { z } from 'zod';
const CreateUserSchema = z.object({
name: z.string().min(2),
email: z.string().email()
});
type CreateUserDto = z.infer<typeof CreateUserSchema>;
有关详细模式,请参阅 react-integration.md 和 nestjs-integration.md。
import { z } from 'zod';
// 定义模式
const UserSchema = z.object({
id: z.string().uuid(),
name: z.string().min(1).max(100),
email: z.string().email(),
role: z.enum(["user", "admin", "moderator"]),
createdAt: z.coerce.date()
});
// 从模式推断 TypeScript 类型
type User = z.infer<typeof UserSchema>;
// 在运行时验证
function parseUser(data: unknown): User {
return UserSchema.parse(data); // 如果无效则抛出 ZodError
}
// 安全解析(返回结果对象)
const result = UserSchema.safeParse(data);
if (result.success) {
console.log(result.data); // 类型为 User
} else {
console.error(result.error.issues);
}
| 工具 | 版本 | 用途 |
|---|---|---|
| TypeScript | 5.9+ | 类型检查和编译 |
| Node.js | 22 LTS | 运行时环境 |
| Vite | 7.x | 构建工具和开发服务器 |
| pnpm | 9.x | 包管理器 |
| ESLint | 9.x | 使用扁平配置进行代码检查 |
| Vitest | 3.x | 测试框架 |
| Prettier | 3.x | 代码格式化 |
// eslint.config.js
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.strictTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
}
);
allowJs: true 和 checkJs: false.js 重命名为 .ts// 在完全迁移之前,使用 JSDoc
/**
* @param {string} name
* @param {number} age
* @returns {User}
*/
function createUser(name, age) {
return { name, age };
}
有关全面的迁移指南,请参阅 enterprise-patterns.md。
| 错误 | 问题 | 修复方法 |
|---|---|---|
随意使用 any | 破坏了类型安全 | 使用 unknown 并进行类型收窄 |
| 忽略严格模式 | 遗漏 null/undefined 错误 | 启用所有严格选项 |
类型断言 (as) | 可能隐藏类型错误 | 使用 satisfies 或类型守卫 |
| 对简单联合使用 Enum | 生成运行时代码 | 改用字面量联合 |
| 不验证 API 数据 | 运行时类型不匹配 | 在边界处使用 Zod |
| 特性 | TypeScript | Java | Python |
|---|---|---|---|
| 类型系统 | 结构类型 | 名义类型 | 渐进式(鸭子类型) |
| 可空性 | 显式 (`T | null`) | @Nullable 注解 |
| 泛型 | 类型级别,擦除 | 类型级别,擦除 | 运行时通过 typing |
| 接口 | 结构匹配 | 必须实现 | 协议 (3.8+) |
| 枚举 | 避免使用(改用联合) | 一等公民 | Enum 类 |
每周安装次数
353
代码仓库
GitHub 星标数
10
首次出现
2026年1月25日
安全审计
安装于
github-copilot315
opencode310
codex306
cursor305
gemini-cli302
amp295
Build enterprise-grade, type-safe applications with TypeScript 5.9+.
Compatibility: TypeScript 5.9+, Node.js 22 LTS, Vite 7, NestJS 11, React 19
# Initialize TypeScript project with ESM
pnpm create vite@latest my-app --template vanilla-ts
cd my-app && pnpm install
# Configure strict TypeScript
cat > tsconfig.json << 'EOF'
{
"compilerOptions": {
"target": "ES2024",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"esModuleInterop": true,
"skipLibCheck": true
}
}
EOF
Use when:
Before starting any TypeScript project:
- [ ] Use pnpm for package management (faster, disk-efficient)
- [ ] Configure ESM-first (type: "module" in package.json)
- [ ] Enable strict mode in tsconfig.json
- [ ] Set up ESLint with @typescript-eslint
- [ ] Add Prettier for consistent formatting
- [ ] Configure Vitest for testing
const name: string = "Alice";
const age: number = 30;
const active: boolean = true;
const id: bigint = 9007199254740991n;
const key: symbol = Symbol("unique");
// Union: value can be one of several types
type Status = "pending" | "approved" | "rejected";
// Intersection: value must satisfy all types
type Employee = Person & { employeeId: string };
// Discriminated union for type-safe handling
type Result<T> =
| { success: true; data: T }
| { success: false; error: string };
function handleResult<T>(result: Result<T>): T | null {
if (result.success) {
return result.data; // TypeScript knows data exists here
}
console.error(result.error);
return null;
}
// typeof guard
function process(value: string | number): string {
if (typeof value === "string") {
return value.toUpperCase();
}
return value.toFixed(2);
}
// Custom type guard
interface User { type: "user"; name: string }
interface Admin { type: "admin"; permissions: string[] }
function isAdmin(person: User | Admin): person is Admin {
return person.type === "admin";
}
satisfies Operator (TS 5.0+)Validate type conformance while preserving inference:
// Problem: Type assertion loses specific type info
const colors1 = {
red: "#ff0000",
green: "#00ff00"
} as Record<string, string>;
colors1.red.toUpperCase(); // OK, but red could be undefined
// Solution: satisfies preserves literal types
const colors2 = {
red: "#ff0000",
green: "#00ff00"
} satisfies Record<string, string>;
colors2.red.toUpperCase(); // OK, and TypeScript knows red exists
function first<T>(items: T[]): T | undefined {
return items[0];
}
const num = first([1, 2, 3]); // number | undefined
const str = first(["a", "b"]); // string | undefined
interface HasLength {
length: number;
}
function logLength<T extends HasLength>(item: T): T {
console.log(item.length);
return item;
}
logLength("hello"); // OK: string has length
logLength([1, 2, 3]); // OK: array has length
logLength(42); // Error: number has no length
interface ApiResponse<T> {
data: T;
status: number;
timestamp: Date;
}
async function fetchUser(id: string): Promise<ApiResponse<User>> {
const response = await fetch(`/api/users/${id}`);
const data = await response.json();
return {
data,
status: response.status,
timestamp: new Date()
};
}
| Type | Purpose | Example |
|---|---|---|
Partial<T> | All properties optional | Partial<User> |
Required<T> | All properties required | Required<Config> |
Pick<T, K> | Select specific properties | `Pick<User, "id" |
Omit<T, K> | Exclude specific properties |
// Basic conditional type
type IsString<T> = T extends string ? true : false;
// Extract array element type
type ArrayElement<T> = T extends (infer E)[] ? E : never;
type Numbers = ArrayElement<number[]>; // number
type Strings = ArrayElement<string[]>; // string
// Practical: Extract Promise result type
type UnwrapPromise<T> = T extends Promise<infer R> ? R : T;
// Make all properties readonly
type Immutable<T> = {
readonly [K in keyof T]: T[K];
};
// Make all properties nullable
type Nullable<T> = {
[K in keyof T]: T[K] | null;
};
// Create getter functions for each property
type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
interface Person { name: string; age: number }
type PersonGetters = Getters<Person>;
// { getName: () => string; getAge: () => number }
// Typed functional component
interface ButtonProps {
label: string;
onClick: () => void;
variant?: "primary" | "secondary";
}
const Button: React.FC<ButtonProps> = ({ label, onClick, variant = "primary" }) => (
<button className={variant} onClick={onClick}>
{label}
</button>
);
// Typed hooks
const [count, setCount] = useState<number>(0);
const userRef = useRef<HTMLInputElement>(null);
// Type-safe DTO with class-validator
import { IsString, IsEmail, MinLength } from 'class-validator';
class CreateUserDto {
@IsString()
@MinLength(2)
name: string;
@IsEmail()
email: string;
}
// Or with Zod (modern approach)
import { z } from 'zod';
const CreateUserSchema = z.object({
name: z.string().min(2),
email: z.string().email()
});
type CreateUserDto = z.infer<typeof CreateUserSchema>;
See react-integration.md and nestjs-integration.md for detailed patterns.
import { z } from 'zod';
// Define schema
const UserSchema = z.object({
id: z.string().uuid(),
name: z.string().min(1).max(100),
email: z.string().email(),
role: z.enum(["user", "admin", "moderator"]),
createdAt: z.coerce.date()
});
// Infer TypeScript type from schema
type User = z.infer<typeof UserSchema>;
// Validate at runtime
function parseUser(data: unknown): User {
return UserSchema.parse(data); // Throws ZodError if invalid
}
// Safe parsing (returns result object)
const result = UserSchema.safeParse(data);
if (result.success) {
console.log(result.data); // Typed as User
} else {
console.error(result.error.issues);
}
| Tool | Version | Purpose |
|---|---|---|
| TypeScript | 5.9+ | Type checking and compilation |
| Node.js | 22 LTS | Runtime environment |
| Vite | 7.x | Build tool and dev server |
| pnpm | 9.x | Package manager |
| ESLint | 9.x | Linting with flat config |
| Vitest | 3.x | Testing framework |
| Prettier | 3.x | Code formatting |
// eslint.config.js
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.strictTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
}
);
allowJs: true and checkJs: false to tsconfig.json.js to .ts one at a time// Before full migration, use JSDoc
/**
* @param {string} name
* @param {number} age
* @returns {User}
*/
function createUser(name, age) {
return { name, age };
}
See enterprise-patterns.md for comprehensive migration guides.
| Mistake | Problem | Fix |
|---|---|---|
Using any liberally | Defeats type safety | Use unknown and narrow |
| Ignoring strict mode | Misses null/undefined bugs | Enable all strict options |
Type assertions (as) | Can hide type errors | Use satisfies or guards |
| Enum for simple unions | Generates runtime code | Use literal unions instead |
| Not validating API data | Runtime type mismatches | Use Zod at boundaries |
| Feature | TypeScript | Java | Python |
|---|---|---|---|
| Type System | Structural | Nominal | Gradual (duck typing) |
| Nullability | Explicit (`T | null`) | @Nullable annotations |
| Generics | Type-level, erased | Type-level, erased | Runtime via typing |
| Interfaces | Structural matching | Must implement | Protocol (3.8+) |
| Enums | Avoid (use unions) | First-class | Enum class |
Weekly Installs
353
Repository
GitHub Stars
10
First Seen
Jan 25, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
github-copilot315
opencode310
codex306
cursor305
gemini-cli302
amp295
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
106,200 周安装
Omit<User, "password">Record<K, V> | Object with typed keys/values | Record<string, number> |
ReturnType<F> | Extract function return type | ReturnType<typeof fn> |
Parameters<F> | Extract function parameters | Parameters<typeof fn> |
Awaited<T> | Unwrap Promise type | Awaited<Promise<User>> |