Loading...
Loading...
Compare original and translation side by side
// ❌
const name: string = "John";
const user: { name: string; age: number } = { name: "John", age: 30 };
// ✅
const name = "John";
const user = { name: "John", age: 30 };// ❌
const name: string = "John";
const user: { name: string; age: number } = { name: "John", age: 30 };
// ✅
const name = "John";
const user = { name: "John", age: 30 };function calculateTotal(items: Item[]): number {
return items.reduce((sum, item) => sum + item.price, 0);
}
async function fetchUser(id: string): Promise<User> {
const res = await fetch(`/api/users/${id}`);
return res.json();
}function calculateTotal(items: Item[]): number {
return items.reduce((sum, item) => sum + item.price, 0);
}
async function fetchUser(id: string): Promise<User> {
const res = await fetch(`/api/users/${id}`);
return res.json();
}unknown// ❌
function process(data: any) {
return data.value;
}
// ✅
function process(data: unknown): number {
if (typeof data === "object" && data !== null && "value" in data)
return (data as { value: number }).value;
throw new Error("Invalid data");
}unknown// ❌
function process(data: any) {
return data.value;
}
// ✅
function process(data: unknown): number {
if (typeof data === "object" && data !== null && "value" in data)
return (data as { value: number }).value;
throw new Error("Invalid data");
}TK extends keyof TgetProperty<T, K extends keyof T>(obj: T, key: K): T[K]TK extends keyof TgetProperty<T, K extends keyof T>(obj: T, key: K): T[K]typeofinstanceofin(value): value is T템플릿:assets/types.guards.ts
typeofinstanceofin(value): value is T模板:assets/types.guards.ts
템플릿:
assets/tsconfig.nextjs.tsassets/tsconfig.node.tsassets/tsconfig.react.ts
模板:
assets/tsconfig.nextjs.tsassets/tsconfig.node.tsassets/tsconfig.react.ts
템플릿:assets/types.events.ts
模板:assets/types.events.ts
템플릿:assets/types.utils.ts
模板:assets/types.utils.ts
interfacetype/** 비활성화 상태 */as consttypeof obj[keyof typeof obj]type UserId = string & { readonly brand: unique symbol }asT extends objectinterfacetype/** 禁用状态 */as consttypeof obj[keyof typeof obj]type UserId = string & { readonly brand: unique symbol }asT extends object| 에러 | 대응 |
|---|---|
| 타입 가드로 분기 후 할당 |
| 타입 확장 또는 |
| |
| 제네릭 |
| |
as| 错误 | 对应解决方法 |
|---|---|
| 通过类型守卫分支后赋值 |
| 类型扩展或设置为 |
| 使用 |
| 使用泛型 |
| 使用 |
as이 문서들은 규칙이 아니라 참고용, 판단 기준은 각 skill 문서를 우선
本文档为参考而非规则,判断标准优先遵循各skill文档