【问题标题】:Deno: unable to import a library which contains relative importsDeno:无法导入包含相对导入的库
【发布时间】:2021-07-28 16:48:56
【问题描述】:

我正在尝试编写一些使用Denordflib 的代码。并且惨败。

这是我的测试程序:

// @deno-types="https://dev.jspm.io/npm:rdflib@2.2.7/lib/index.d.ts"
import { Namespace } from 'https://dev.jspm.io/npm:rdflib@2.2.7/lib/index'

当我要求 deno 缓存远程包时,它失败了:

$ deno --unstable cache rdflib.ts
Check file:///home/ian/projects/personal/deno-experiments/rdflib.ts
error: TS2502 [ERROR]: 'thisArg' is referenced directly or indirectly in its own type annotation.
    bind<T>(this: T, thisArg: ThisParameterType<T>): OmitThisParameter<T>;
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    at asset:///lib.es5.d.ts:350:22

TS2614 [ERROR]: Module '"https://dev.jspm.io/npm:rdflib@2.2.7/lib/query"' has no exported member 'Query'. Did you mean to use 'import Query from "https://dev.jspm.io/npm:rdflib@2.2.7/lib/query"' instead?
import { Query } from './query';
         ~~~~~
    at https://dev.jspm.io/npm:rdflib@2.2.7/lib/index.d.ts:16:10

TS2614 [ERROR]: Module '"https://dev.jspm.io/npm:rdflib@2.2.7/lib/updates-via"' has no exported member 'UpdatesSocket'. Did you mean to use 'import UpdatesSocket from "https://dev.jspm.io/npm:rdflib@2.2.7/lib/updates-via"' instead?
import { UpdatesSocket } from './updates-via';
         ~~~~~~~~~~~~~
    at https://dev.jspm.io/npm:rdflib@2.2.7/lib/index.d.ts:26:10

TS2614 [ERROR]: Module '"https://dev.jspm.io/npm:rdflib@2.2.7/lib/updates-via"' has no exported member 'UpdatesVia'. Did you mean to use 'import UpdatesVia from "https://dev.jspm.io/npm:rdflib@2.2.7/lib/updates-via"' instead?
import { UpdatesVia } from './updates-via';
         ~~~~~~~~~~
    at https://dev.jspm.io/npm:rdflib@2.2.7/lib/index.d.ts:27:10

TS2749 [ERROR]: 'Store' refers to a value, but is being used as a type here. Did you mean 'typeof Store'?
    at https://dev.jspm.io/npm:rdflib@2.2.7/lib/index.d.ts:32:32

... many more lines ...

TS2614 [ERROR]: Module '"https://dev.jspm.io/npm:rdflib@2.2.7/lib/utils/termValue"' has no exported member 'termValue'. Did you mean to use 'import termValue from "https://dev.jspm.io/npm:rdflib@2.2.7/lib/utils/termValue"' instead?
export { termValue } from './utils/termValue';
         ~~~~~~~~~
    at https://dev.jspm.io/npm:rdflib@2.2.7/lib/index.d.ts:40:10

Found 44 errors.

据我所知,问题在于远程代码中执行相对导入的行。这种相对导入是否不适用于 Deno,或者我错过了一些重要的步骤,还是我的方法注定要失败?

版本信息:

$ deno --version
deno 1.12.2 (release, x86_64-unknown-linux-gnu)
v8 9.2.230.14
typescript 4.3.5

【问题讨论】:

    标签: import rdf deno


    【解决方案1】:

    问题不在于它们是相对说明符,而在于它们不是完全限定的。来自手册中的第 6.6 节:

    Can I use TypeScript not written for Deno?

    也许吧。这是最好的答案,我们害怕。由于很多原因,Deno 选择了完全合格的模块说明符。部分原因是它将 TypeScript 视为一流的语言。此外,Deno 使用显式模块解析,没有 magic。这实际上与浏览器本身的工作方式相同,尽管它们显然不直接支持 TypeScript。如果 TypeScript 模块使用没有考虑这些设计决策的导入,它们可能无法在 Deno 下工作。

    此外,在最近的 Deno 版本中(从 1.5 开始),我们已经开始使用 Rust 库在某些场景下将 TypeScript 转换为 JavaScript。因此,TypeScript 在某些情况下需要类型信息,因此 Deno 不支持这些情况。如果您将tsc 用作独立设备,则要使用的设置为"isolatedModules" 并将其设置为true 以帮助确保您的代码可以被Deno 正确处理。

    处理扩展和缺少 Node.js 非标准解析逻辑的一种方法是使用import maps,这将允许您指定裸说明符的“包”,然后 Deno 可以解析和加载这些说明符。

    【讨论】:

      猜你喜欢
      • 2021-10-13
      • 2018-11-25
      • 2018-08-05
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 2011-12-17
      • 2021-07-14
      相关资源
      最近更新 更多