[英]Using jsbi in a typescript project with strict mode
I'm working on a typescript library that is using native BigInt
s.我正在使用本机
BigInt
的 typescript 库。 It works great in chrome, but not in Safari.它在 chrome 中效果很好,但在 Safari 中效果不佳。 So I came across the
jsbi
"polyfill" that takes care of this.所以我遇到了解决这个问题的
jsbi
“polyfill”。
But for some reason, I can't import the library correctly.但由于某种原因,我无法正确导入库。
I first tried to import it like this:我首先尝试像这样导入它:
import JSBI from 'jsbi'
The types are correct, but at runtime JSBI
is undefined and I cannot call any methods on it.类型是正确的,但是在运行时
JSBI
是未定义的,我不能在它上面调用任何方法。 (eg. TypeError: Cannot read property 'subtract' of undefined
) (例如
TypeError: Cannot read property 'subtract' of undefined
)
So I tried doing:所以我试着做:
import * as JSBI from 'jsbi'
But then it wouldn't even compile, I get various errors like Cannot use namespace 'JSBI' as a type.
但后来它甚至无法编译,我得到了各种错误,比如
Cannot use namespace 'JSBI' as a type.
. . I also tried to access the methods like this
JSBI.JSBI.subtract
, but then I get the error Property 'JSBI' does not exist on type 'typeof import("/project/node_modules/jsbi/jsbi")'.
我也尝试访问像这样的方法
JSBI.JSBI.subtract
,但后来我得到错误Property 'JSBI' does not exist on type 'typeof import("/project/node_modules/jsbi/jsbi")'.
The only way I got it working was by using the following import:我让它工作的唯一方法是使用以下导入:
import * as JSBI from 'jsbi/dist/jsbi-umd.js'
This actually worked.这实际上奏效了。 JSBI is defined at runtime and I can access its methods.
JSBI 是在运行时定义的,我可以访问它的方法。 However, it only works when strict mode is disabled.
但是,它仅在禁用严格模式时才有效。 When I enable it, i get the error
当我启用它时,我得到了错误
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';`
I event tried to declare my own.d.ts module as a temporary fix, but it didn't work work either.我试图将自己的.d.ts 模块声明为临时修复,但它也不起作用。
Am I missing something here?我在这里错过了什么吗?
JSBI has been migrated to Typescript and includes adts file. JSBI 已迁移到 Typescript 并包含 adts 文件。 This problem should be resolved.
这个问题应该得到解决。 https://github.com/GoogleChromeLabs/jsbi/pull/67
https://github.com/GoogleChromeLabs/jsbi/pull/67
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.