小编coo*_*ood的帖子

为什么 TypesScript 允许无效比较 - boolean === undefined?

面对TS的奇怪行为。

const isItLanding = false;

if (isItLanding === undefined) { // valid
  return ...;
}
Run Code Online (Sandbox Code Playgroud)

但在这儿

const isItLanding = 1;

if (isItLanding === 'undefined') { // error
  return ...;
}
Run Code Online (Sandbox Code Playgroud)

为什么 TS 不保证不会写入无效比较?我怎样才能改变这种行为?

我的 TS 配置如下:

{
  "compilerOptions": {
    "strict": true,
    "target": "esnext",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "importsNotUsedAsValues": "error",
    "allowSyntheticDefaultImports": true,
    "incremental": true,
    "tsBuildInfoFile": ".next/cache/.tscache/",
    "jsx": "preserve",
    "sourceMap": true,
    "baseUrl": ".",
    "paths": …
Run Code Online (Sandbox Code Playgroud)

javascript typescript eslint tslint typescript-typings

4
推荐指数
1
解决办法
368
查看次数