在打字稿中为嵌套命名空间创建类型 [英] Create typings for nested namespaces in typescript

查看:29
本文介绍了在打字稿中为嵌套命名空间创建类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为已经存在的库创建类型定义,并且在正确描述其结构时遇到问题.问题是嵌套的命名空间.在 JavaScript 中,我的方法的全名如下所示:

I'm trying to create typings definitions for already existed library, and having issues describe its structure properly. The problem is nested namespaces. In JavaScript full name of my method looks like this:

NameSpace1.NameSpace2.NameSpace3.MethodName()

但我无法创建正确的 d.ts 定义文件...我尝试了不同的组合,使用 export interfaceexport 模块 用于嵌套命名空间...但没有任何效果.唯一不会触发任何错误的结构如下:

But I'm not able to create proper d.ts definition file... I've tried different combinations, with export interface, export module for nested namespaces... But nothing works. The only construction that does not fire any errors is following:

declare namespace NameSpace1 {
}

但这还不够……你知道如何在 TypeScript 定义中正确描述这样一个嵌套的命名空间吗?

But this is not enough... Do you know how to properly describe such a nested namespaces in TypeScript definitions?

推荐答案

declare namespace NameSpace1.NameSpace2.NameSpace3 {
  function MethodName(): void;
}

https://github.com/DefinitelyTyped/DefinitelyTyped 上有成千上万个关于如何编写定义文件的示例 ;我建议您在下次遇到困难时寻找类似于您所拥有的东西.

There are literally thousands of examples of how to write definition files at https://github.com/DefinitelyTyped/DefinitelyTyped ; I'd recommend looking for something similar to what you have next time you're stuck.

这篇关于在打字稿中为嵌套命名空间创建类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆