Loading...
Loading...
Compare original and translation side by side
strict: truestrict: truetsconfig.jsontsconfig.json{
"compilerOptions": {
/* Type Safety */
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"useUnknownInCatchVariables": true,
"noUncheckedIndexedAccess": true, /* 2025 Essential: Prevents accessing undefined array indices */
"exactOptionalPropertyTypes": true, /* Stricter optional property checks */
/* Modules & Emit */
"module": "NodeNext", /* or "ESNext" for pure frontend */
"moduleResolution": "NodeNext", /* Aligns with modern Node.js ESM */
"target": "ES2022", /* Modern runtimes support recent ES features */
"skipLibCheck": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"verbatimModuleSyntax": true, /* Enforces strict import/export syntax (TS 5.0+) */
/* Developer Experience */
"allowSyntheticDefaultImports": true
}
}{
"compilerOptions": {
/* Type Safety */
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"useUnknownInCatchVariables": true,
"noUncheckedIndexedAccess": true, /* 2025必备:防止访问未定义的数组索引 */
"exactOptionalPropertyTypes": true, /* 更严格的可选属性检查 */
/* Modules & Emit */
"module": "NodeNext", /* 纯前端项目可使用"ESNext" */
"moduleResolution": "NodeNext", /* 适配现代Node.js ESM规范 */
"target": "ES2022", /* 现代运行时支持最新ES特性 */
"skipLibCheck": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"verbatimModuleSyntax": true, /* 强制执行严格的导入/导出语法(TS 5.0+支持) */
/* Developer Experience */
"allowSyntheticDefaultImports": true
}
}unknownanyunknownanyanyunknown// ❌ Bad
function processData(input: any) {
input.doSomething(); // Runtime crash risk
}
// ✅ Good
function processData(input: unknown) {
if (typeof input === 'string') {
console.log(input.toUpperCase()); // Safe
} else if (isCustomType(input)) {
input.doSomething(); // Safe via type guard
}
}anyunknown// ❌ 错误示例
function processData(input: any) {
input.doSomething(); // 存在运行时崩溃风险
}
// ✅ 正确示例
function processData(input: unknown) {
if (typeof input === 'string') {
console.log(input.toUpperCase()); // 类型安全
} else if (isCustomType(input)) {
input.doSomething(); // 通过类型守卫保证安全
}
}satisfiessatisfiessatisfiestype Config = Record<string, string | number>;
const myConfig = {
port: 8080,
host: "localhost"
} satisfies Config;
// ✅ TS knows 'port' is a number directly, no casting needed.
myConfig.port.toFixed(2); satisfiestype Config = Record<string, string | number>;
const myConfig = {
port: 8080,
host: "localhost"
} satisfies Config;
// ✅ TS可以直接识别'port'是数字类型,无需强制转换
myConfig.port.toFixed(2); readonlyinterface User {
readonly id: string;
readonly name: string;
tags: readonly string[]; // Immutable array
}
function printTags(tags: readonly string[]) {
// tags.push("new"); // ❌ Error: Property 'push' does not exist on type 'readonly string[]'
}readonlyinterface User {
readonly id: string;
readonly name: string;
tags: readonly string[]; // 不可变数组
}
function printTags(tags: readonly string[]) {
// tags.push("new"); // ❌ 报错:类型'readonly string[]'上不存在属性'push'
}type EventName = "click" | "hover";
type HandlerName = `on${Capitalize<EventName>}`; // "onClick" | "onHover"type EventName = "click" | "hover";
type HandlerName = `on${Capitalize<EventName>}`; // 结果为"onClick" | "onHover"import type { ... }import { type ... }verbatimModuleSyntaxawait import(...)import type { ... }import { type ... }verbatimModuleSyntaxawait import(...)aszodasFunctionobject() => voidRecord<string, unknown>type Status = 'open' | 'closed'enumaszodasFunctionobject() => voidRecord<string, unknown>type Status = 'open' | 'closed'enum