繁体   English   中英

全局变量作为命名空间? 为 staruml 定义环境打字稿定义

[英]global variable as a namespace ? defining ambient typescript definitions for staruml

我正在尝试为StarUML 工具定义打字稿类型定义。 我已经设法使它适用于 API 的很大一部分,但我遇到了以下问题:如何在(javascript)全局变量(下面的“type”)和包含类的 typescript 命名空间之间建立链接?

(一)要解决的问题

StarUML 提供了一种全局变量type ,可以注册数百个来自未知地方的类。 例如type.Element是一个类(不是元素!)以及type.Model 下面我们以这两种类型为例。

在 javascript 中,这些类主要用于if (x instanceof type.Element)类的语句中。

使用打字稿,我希望能够定义像f(e : type.Element)这样的签名(我很乐意删除类型前缀,但这是另一回事)并且希望对myElement._id_id是类Element的一个属性。

(B) 第一次尝试:将“类型”建模为变量

我首先尝试将类型定义为变量(因为这实际上是一个变量):

// type.d.ts

declare class Element {
    _id: string
    // ...
}
declare class Model extends Element {
    name: string
    // ...
}

declare const type = {
    "Element" = Element,
    "Model" = Model
    // ...
}

这不起作用,因为这会产生以下错误: S1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference 我提到这个解决方案是因为它清楚地说明了type是什么:一个为每个类名(字符串)提供的寄存器,类本身。 这些类是在未知位置的其他地方定义的。

(C) 第二次尝试:将“类型”建模为命名空间。

在阅读打字稿文档后,经过各种试验,我想出了以下打字稿文件types.d.ts (那可能是我错了)。

// types.ts
export namespace type {
    class Element {
        _id: string
        // ...
    }
    class Model extends Element {
        name: string
    }
    // ...
}

(D) 客户代码

下面是使用此 API 定义的示例代码 ( main.ts )。 为了简化文件type.d.tsmain.ts都在顶层。

// (1)     /// <reference path="./types.d.ts" />
// (2)     import {type} from "./types"
// (3)     declare var type

function hello(m: type.Element): void {
    console.log("    hello: (" + e._id + ')')
}
console.log(type)
console.log(type.Element)

我没有设法“让它工作”我尝试了各种组合,取消了前 3 行中的一些(见下文)。

(D.2) 我的期望:

  • (a) function hello中的类型应正确定义(打字稿)
  • (b) 智能感知应该在下一行e._id (typescript) 上起作用
  • (c) 最后一行应该显示type.Element类 (javascript)

无论“导入”第一行如何,我都无法让它同时工作。

(D.3) 我得到了什么:

  • (1) 我没有设法使第 (1) 行/// <reference ... “工作”。 我还尝试了论坛中提供的解决方案,例如将tsconfigtypeRootspaths一起使用。 我不知道解决方案是否应该来自那里。

  • (2) import {type} ...对于命名空间是可以的,但是行console.log(type.element)在运行时返回 undefined。

  • (3) declare var type使 javascript 代码运行正常但与 (2) 冲突。

当 (2) 和 (3) 同时存在时,由于type as namespace 和type as variable 之间的冲突,会产生 typescript 错误。

(D.4) 解决方案是什么?

在阅读了打字稿文档和其他一些博客后,我仍然感到困惑。 我不知道问题是否出在我的方法(C)中,“将变量类型建模”为命名空间,或者我不知道如何在编译/运行时调用此命名空间/变量。

非常欢迎任何信息或指向该信息的指针! 谢谢大家。

警告:我在 TypeScript 方面只是中等水平,而在.d.ts文件方面则不太好。 但这在本地工作,并且似乎与文档相匹配,特别是这个示例

一个.d.ts文件定义环境声明,你不要在其中使用export (据我所知),你只是在其中声明东西,因为目的是声明已经存在的东西(在你的情况下,因为SmartUML 库在运行时创建该type全局及其属性)。

因此,在您的.d.ts文件中,您声明一个包含这些类的命名空间:

/**
 * SmartUML namespace global.
 */
declare namespace type {
    /**
     * SmartUML `Element` class.
     */
    class Element {
        /**
         * The ID fo the element.
         */
        _id: string;
        // ...
    }
    /**
     * SmartUML `Model` class.
     */
    class Model extends Element {
        /**
         * The name of the model.
         */
        name: string;
        // ...
    }
    // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM