Search code examples
typescripttensorflow.js

TFJS throws TS2411 as error when importing using Typescript


I try to import tfjs in a Typescript environment. However, I get the following error:

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'.

This can be easily reproduced with the following steps:

npm init
npm i @tensorflow/tfjs
npm i typescript

Create a index.ts:

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

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

Configure and compile:

tsc --init
tsc

The package.json then contains the following:

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

And the tsconfig looks like this:

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

Solution

  • After some interaction with the tfjs team, we could track down the issue. The problem indeed is in the typescript configuration. If the flag: "strictNullChecks": true is set, typescript throws the aforementioned compilation error. Since this flag is included in "strict": true, which in turn is included in the default typescript configuration, this is unwanted behaviour.

    As a temporary workaround, one can use skipLibCheck to only check their own code and not the dependencies.

    The Issue related to this can be found here: Issue on tfjs