使用 Typescript 导入时,TFJS 抛出 TS2411 作为错误

TFJS throws TS2411 as error when importing using Typescript

我尝试在 Typescript 环境中导入 tfjs。但是,我收到以下错误:

node_modules/@tensorflow/tfjs-layers/dist/keras_format/types.d.ts:12:5 - error TS2411: Property 'config' of type 'T' is not assignable to string index type 'PyJsonValue'.

这可以通过以下步骤轻松重现:

npm init
npm i @tensorflow/tfjs
npm i typescript

创建 index.ts:

import * as tf from '@tensorflow/tfjs';

// Define a model for linear regression.
const model = tf.sequential();

配置编译:

tsc --init
tsc

然后 package.json 包含以下内容:

"dependencies": {
  "@tensorflow/tfjs": "^0.15.1",
  "typescript": "3.3.3"
}

tsconfig 看起来像这样:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",                     
    "strict": true,                           
    "esModuleInterop": true                   
  }
}

在与 tfjs 团队进行了一些互动之后,我们可以找到问题所在。问题确实出在打字稿配置中。 如果标志:"strictNullChecks": true 被设置,打字稿会抛出上述编译错误。由于此标志包含在 "strict": true 中,而后者又包含在默认的打字稿配置中,因此这是不需要的行为。

作为一种临时解决方法,可以使用 skipLibCheck 只检查自己的代码而不检查依赖项。

与此相关的问题可以在这里找到:Issue on tfjs