简体   繁体   English

在严格模式的 typescript 项目中使用 jsbi

[英]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.

相关问题 打字稿错误:在严格模式下,块中不允许使用函数声明 - Typescript error : Function declarations are not permitted in blocks in strict mode Typescript:在严格模式下分配之前使用了变量 - Typescript: Variable is used before being assigned in strict mode 在 TypeScript 中以严格模式传递给定回调函数中的其余参数 - Pass rest of arguments in a given callback function in TypeScript in strict mode JavaScript:可以使用单引号('use strict')启用ECMAScript 5的严格模式(“use strict”)吗? - JavaScript: Can ECMAScript 5's Strict Mode (“use strict”) be enabled using single quotes ('use strict')? 如何修复不使用显式注释且无法在严格模式下调用的 Angular - how to fix Angular not using explicit annotation and cannot be invoked in strict mode templateUrl函数未使用显式注释,因此无法在严格模式下调用 - templateUrl function is not using explicit annotation and cannot be invoked in strict mode 如何在JavaScript中使用严格模式发送控制字符? - How to send control char using strict mode in javascript? 如何在使用strict mode pragma时声明全局变量 - How to declare global variables when using the strict mode pragma Eslint 在使用 babel 解析器时禁用严格模式 - Eslint disable strict mode when using babel parser 操作员在严格模式下更换“with” - Operator replacement “with” in strict mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM