Skip to content

Strict 模式

一句话解释

Strict 模式 (strict: 真 in tsconfig) 启用 family 的 strict 类型-检查 options 包含 noImplicitAny, strictNullChecks, and 更多.

用大白话说

Strict 模式是 shorthand 那个启用多个 strict 类型-检查 options at 一次. 设置 strict: true in tsconfig.json turns on: noImplicitAny, strictNullChecks, strictFunctionTypes, strictBindCallApply, strictPropertyInitialization, noImplicitThis, and alwaysStrict.

Strict mode catches the most common sources of runtime errors: implicit any values, null/undefined access, incorrect this binding, and uninitialized class properties. It is the recommended default for all new TypeScript projects.

While strict mode may produce more compiler errors initially, it leads to more robust, self-documenting code. The errors it catches are almost always real bugs or design issues.

示例

typescript
// With strict: true
function greet(name: string) {
  console.log(name.toUpperCase());
}
greet(null); // Error: Argument of type 'null' not assignable to 'string'

let x; // Error: Variable 'x' implicitly has 'any' type

Strict 模式 catches 空实参 and implicit 任何 declarations.

相关术语

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表