Prompts-MCP 686 skills · ai / design / design-pattern / framework / fundamentals / habit / lang / tech-selection /mcp/sse
lang lang/typescript/typing/strict-mode.md medium

typescript-strict-mode

tsconfig strict + noUncheckedIndexedAccess 全启用。Use when 写 TS 业务代码 / 评审涉及 `strict-mode` 的 PR。

typescriptstricttsconfignouncheckedindexedaccess全启用
paths
  • frontend/**/*.ts
  • frontend/**/*.tsx
  • frontend/tsconfig.json

TypeScript · strict mode

规则

tsconfig.json 必须开 strict + noUncheckedIndexedAccess + exactOptionalPropertyTypes

{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,
    "exactOptionalPropertyTypes": true,
    "noFallthroughCasesInSwitch": true,
    "paths": { "@/*": ["./src/*"] }
  }
}

各项含义

选项 作用 典型修复
strict 总开关(含 strictNullChecks 等 6 项) 必须开
noUncheckedIndexedAccess arr[i] 类型变 T | undefined 加 if 守卫或非空断言(仅在确认安全时)
noImplicitOverride 子类覆盖必须显式 override 关键字 override
exactOptionalPropertyTypes foo?: stringfoo: string | undefined 不要给 optional 字段显式赋 undefined

索引访问示例

const items = ["a", "b", "c"];

// ❌ items[10] 看似 string,运行时是 undefined
const x: string = items[10];        // 编译期通过,运行时崩

// ✅ noUncheckedIndexedAccess 强制类型变成 string | undefined
const x = items[10];                // x: string | undefined
if (x) doSomething(x);

编辑器集成

VSCode:自动启用项目 tsconfig。 CI:

pnpm tsc --noEmit

自检

  • [ ] tsconfig 包含上述 5 个选项?
  • [ ] CI 跑 tsc --noEmit 全绿?
  • [ ] 没有 // @ts-ignore / // @ts-expect-error(必要时带说明注释)?

相关