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

标签 typescript tensorflow.js

我尝试在 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 团队进行了一些互动之后,我们可以追踪到问题所在。问题确实出在 typescript 配置中。 如果标志:"strictNullChecks": true 被设置, typescript 会抛出上述编译错误。由于此标志包含在 "strict": true 中,而后者又包含在默认的 typescript 配置中,因此这是不需要的行为。

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

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

关于typescript - 使用 Typescript 导入时,TFJS 抛出 TS2411 作为错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54675549/

相关文章:

node.js - 类型ORM。两个外键在一个表中引用相同的主键

requirejs - typescript + Require.js : extending prototype of String and importing that module

tensorflow - 如何在tensorflow js转换器中获取单个权重文件

tensorflow.js - 如何将一维张量转换为常规javascript数组?

javascript - 检查输入 : expected dense_Dense1_input to have x dimension(s). 时出错,但得到形状为 y、z 的数组

javascript - Tensorflow.js 中的图像大小调整方法(resizeBilinear 和 resizeNearestNeighbor)不会返回正确的结果

reactjs - 使用 typescript 3.2.2 创建动态组件名称

javascript - 将 Typescript 类导入 Jasmine 测试(<type> 不是构造函数错误)

angular - 未知选项 : '--target' , 未知选项: '--environment' ;尝试在 prod 中构建 ng 应用程序时

javascript - 检查输入 : expected dense_Dense5_input to have 4 dimension(s). 时出错,但得到形状为 5、2、5 的数组