LoginSignup
3
1

More than 3 years have passed since last update.

Tensorflow.js を Typescript で compile error なく動かす

Last updated at Posted at 2019-11-25

tensorflow.js を typescript で楽に扱いたかっただけなんです.

要約

  • typescript: 3.5.3
  • @types/webgl2: 0.0.5
  • tsconfig.json は tfjs 本家をベースにする

当時の環境

  • vue: 2.6.10
  • typescript: 3.6.2
  • vue-property-decorator: 8.1.0
  • tensorflow: 1.3.2

発生したエラーと解決法

原因までは分かんないです.誰か解説してください.

tensorflow.js

Subsequent variable declarations must have the same type.  Variable 'WebGL2RenderingContext' must be of type '{ new (): ...

582 declare var WebGL2RenderingContext: {
                ~~~~~~~~~~~~~~~~~~~~~~

WebGL2RenderingContext で前のと違う型が宣言されているらしいです.探してany にしてるStackoverflowもあった気がします.

私は 公式の tsconfig をコピペしました.
また vue-property-decorator を使っていたので

{
  "compilerOptions": {
    "module": "esnext",
    "target": "esnext",
    "experimentalDecorators": true
  }
}

あたりを書き換え/加えました.

WebGL

Interface 'WebGL2RenderingContext' incorrectly extends interface 'WebGL2RenderingContextBase'.

のようなエラーが3種.

WebGL2RenderingContext が 2 じゃないほうを正しく継承できていないそうです.これも無理やり書き換えてもいいと思います.私は趣味のプロダクトだったので typescript をダウングレードしました.

yarn upgrade typescript@3.5.3
yarn add -D @types/webgl2: 0.0.5

もしやと思って typescript のバージョンを公式と揃えたらたまたまうまくいっただけです.3.4.3 や3.6.3 だとエラーが出ました.

「 typescript のダウングレードはきついっす......」というときは,該当箇所を any にするとか継承せずにそれっぽい型を書き上げるか,ファイル自体をignoreすればいいと思います.たぶん.

3
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1