我正在尝试使用 typescript 开发 gnome shell 扩展。我遇到的问题是导入 .ts 文件时出错。 gnome shell 文档说要设置 tsconfig,如下所示:Gnome typescript 文档:
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "./dist",
"sourceMap": false,
"strict": true,
"target": "ES2022",
"lib": [
"ES2022"
],
},
"include": [
"ambient.d.ts",
],
"files": [
"extension.ts",
"prefs.ts"
],
}
但是如果我在没有扩展名的情况下导入,则会收到以下错误:
1. tsserver: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './constants.js'? [2835]
那么如果我使用扩展名导入
import { SOME_CONSTANT } from './constansts.ts
,我会收到以下错误:
1. tsserver: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled. [5097]
如果我启用此规则,则会收到编译错误:
tsconfig.json:5:35 - error TS5096: Option 'allowImportingTsExtensions' can only be used when either 'noEmit' or 'emitDeclarationOnly' is set.
5 "allowImportingTsExtensions": true,
~~~~
它可以编译,但
.ts
扩展名保留在 import 语句中,因此无法找到编译后的文件。我无法设置任何 noEmit
或 emitDeclarationOnly
设置,因为我需要发出 javascript 文件,因为 Gnome shell 使用纯 js 和 es 导入模块。我尝试将 moduleResolution
更改为 ESNext
以及其他几个,但后来我无法导入任何 gnomes 库。我现在迷路了。任何人都可以帮忙吗?另外,如果有人知道在文档中启用哪些模块,请留下评论,这也会有帮助。
我认为你应该能够在 .ts 文件中指定“constants.js”,引用将要发出的内容,而不是你实际编写的“constants.ts”,这看起来很直观,但这就是我的 TypeScript 的样子(尽管不是 GNOME 扩展的一部分)。