繁体   English   中英

TypeScript 不检查可选参数的兼容性

[英]TypeScript not checking for optional arguments compatibility

尽管我在我的tsconfig.jsonstrict: true ,但这不会出错:

const a: (b?: string) => string[] =
         (c : string) => c.split(',');
           ^
  should scream in vein

我如何让 TypeScript 对此感到恐慌?

软件包版本:

  • 打字稿 2.5.3
  • ts-loader 3.1.1
  • 网络包 3.6.0

这是我完整的tsconfig.json

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "strict": true,
    "noImplicitAny": true,
    "allowJs": true,
    "sourceMap": true,
    "allowSyntheticDefaultImports": false,
    "moduleResolution": "node",
    "noUnusedLocals": true,
    "noUnusedParameters": false,
    "preserveConstEnums": false,
    "removeComments": false,
    "lib": [
      "es5",
      "es6",
      "dom",
      "es2015.core",
      "es2015.collection",
      "es2015.generator",
      "es2015.iterable",
      "es2015.promise",
      "es2015.proxy",
      "es2015.reflect",
      "es2015.symbol",
      "es2015.symbol.wellknown",
      "esnext.asynciterable"
    ]
  },
  "exclude": [
    "node_modules",
    "test",
    ".git"
  ]
}

在您的设置中发生了一些时髦的事情。 我将 TypeScript 2.6.1 与您的tsconfig.json和代码一起使用:

const a: (b?: string) => any = (b: string) => 1;

因为你有strict标志,它包括你需要得到错误的两个标志; strictNullChecksstrictFunctionTypes 当我运行时:

tsc

我收到消息:

app.ts(1,7): error TS2322: Type '(b: string) => number' is not assignable to type '(b?: string | undefined) => any'.   Types of parameters 'b' and 'b' are incompatible.
    Type 'string | undefined' is not assignable to type 'string'.
      Type 'undefined' is not assignable to type 'string'.

你是如何运行编译器的? 我问的原因是,如果您如上所示“直接运行”,它只会使用您的配置文件。 例如,如果您传递文件名参数,您将不会使用您的配置:

tsc app.ts

不会导致任何错误,因为您不再使用tsconfig.json

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM