首页 > 解决方案 > 在严格模式的打字稿项目中使用 jsbi

问题描述

我正在开发一个使用 nativeBigInt的打字稿库。它在 chrome 中运行良好,但在 Safari 中却不行。所以我遇到了解决这个问题的jsbi“polyfill”。

但由于某种原因,我无法正确导入库。

我首先尝试像这样导入它:

import JSBI from 'jsbi'

类型是正确的,但在运行时JSBI是未定义的,我不能在它上面调用任何方法。(例如。TypeError: Cannot read property 'subtract' of undefined

所以我试着做:

import * as JSBI from 'jsbi'

但后来它甚至无法编译,我收到各种错误,例如Cannot use namespace 'JSBI' as a type.. 我也尝试访问这样的方法JSBI.JSBI.subtract,但随后出现错误Property 'JSBI' does not exist on type 'typeof import("/project/node_modules/jsbi/jsbi")'.

我让它工作的唯一方法是使用以下导入:

import * as JSBI from 'jsbi/dist/jsbi-umd.js'

这实际上奏效了。JSBI 是在运行时定义的,我可以访问它的方法。但是,它仅在禁用严格模式时才有效。当我启用它时,我得到了错误

error TS7016: Could not find a declaration file for module 'jsbi/dist/jsbi-umd.js'.
 '/project/node_modules/jsbi/dist/jsbi-umd.js' implicitly has an 'any' type.
  Try `npm install @types/jsbi` if it exists or add a new declaration (.d.ts) file containing `declare module 'jsbi/dist/jsbi-umd.js';`

我试图将我自己的 .d.ts 模块声明为临时修复,但它也不起作用。

我在这里错过了什么吗?

标签: javascripttypescriptbigint

解决方案


JSBI 已迁移到 Typescript 并包含一个 .d.ts 文件。这个问题应该得到解决。https://github.com/GoogleChromeLabs/jsbi/pull/67


推荐阅读