Deno:无法导入包含相关导入的库

Deno: unable to import a library which contains relative imports

我正在尝试编写一些使用 Deno and rdflib 的代码。并且惨遭失败。

这是我的测试程序:

// @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

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

Can I use TypeScript not written for Deno?

Maybe. That is the best answer, we are afraid. For lots of reasons, Deno has chosen to have fully qualified module specifiers. In part this is because it treats TypeScript as a first class language. Also, Deno uses explicit module resolution, with no magic. This is effectively the same way browsers themselves work, though they don't obviously support TypeScript directly. If the TypeScript modules use imports that don't have these design decisions in mind, they may not work under Deno.

Also, in recent versions of Deno (starting with 1.5), we have started to use a Rust library to do transformations of TypeScript to JavaScript in certain scenarios. Because of this, there are certain situations in TypeScript where type information is required, and therefore those are not supported under Deno. If you are using tsc as stand-alone, the setting to use is "isolatedModules" and setting it to true to help ensure that your code can be properly handled by Deno.

One of the ways to deal with the extension and the lack of Node.js non-standard resolution logic is to use import maps which would allow you to specify "packages" of bare specifiers which then Deno could resolve and load.