这个问题与 Kendo UI 没有直接关系,而是更多关于 TypeScript 命名空间声明。
我正在尝试从 Kendo UI 库导入一些界面。 Kendo UI的index.d.ts结构如下:
declare namespace kendo.ui {
class Draggable extends kendo.ui.Widget { ... }
interface DraggableEvent { ... }
// ... other declarations
}
现在我将此声明导入到我的 TypeScript 文件中,如下所示:
import '@progress/kendo-ui';
奇怪的是,在我的 TypeScript 文件中,我只能看到类和函数声明,从命名空间外部看不到任何接口。
这就是我的 tsconfig.json 的配置方式:
{
"compilerOptions": {
"lib": [
"dom",
"es5",
"es2015",
"es2015.promise"
],
"module": "es2015",
"moduleResolution": "node",
"strict": false,
"target": "es2015",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true
}
}
我尝试在一些接口声明之前添加
export
关键字,但没有成功。
有人可以解释一下这是怎么回事吗?
同样的问题,如何通过命名空间访问接口,例如:
let obj: namespace.ISomeInterface
现在只有类、函数和属性可以通过命名空间访问。 接口需要手动导入
import type { ISomeInterface } from "xxx.d.ts"
Typescript 中的命名空间只是一个对象包装器。它将您的类和接口放置在具有您声明的名称的对象中。 这是名称为
kendo.ui
的 Typescript 命名空间示例。
顺便说一句,您不必导入 .d.ts
文件。
declare namespace kendo.ui {
class Draggable {}
interface DraggableEvent {}
}
class Test{
constructor() {
let d = new kendo.ui.Draggable();
}
}