简体   繁体   English

为TypeScript声明节点模块,该模块在typings中不可用

[英]Declare node module for TypeScript that is not available within typings

I'm quite new to TypeScript and am trying to script a little cli app. 我是TypeScript的新手,我正在尝试编写一个小的cli应用程序。

I want to use npm~command-line-args but its definition is not available with typings so I thought how hard could it be to create my own. 我想使用npm~command-line-args,但它的定义不适用于typings所以我觉得创建自己的有多难。 Turns out it is harder than I expected. 事实证明它比我预期的要难。

So my goal is to export a module which exposes just a single function and maybe a small type declaration. 所以我的目标是导出一个模块,它只公开一个函数,也许是一个小型的声明。

I tried this so far: 到目前为止我试过这个:

declare module 'command-line-args' {
  export interface cliOptionDefinition {
    name: string,
    alias?: string,
    type: BooleanConstructor|StringConstructor|NumberConstructor,
    multiple?: Boolean,
    defaultOption?: any
  }

  export = function commandLineArgs <R> ( optionDefinitions: cliOptionDefinition[] ) {};
}

But am getting this error: 但是我收到了这个错误:
error TS1183: An implementation cannot be declared in ambient contexts.

Does: 请问:

export function commandLineArgs <R> ( optionDefinitions: cliOptionDefinition[] );

work the way that you would expect? 以你期望的方式工作? (Omit the = , and the {} .) If you are intending to provide an implementation of commandLineArgs , then you want to export it from a non-ambient (remove declare etc.), preferably external, module (or, alternatively, namespace). (省略={} 。)如果您打算提供commandLineArgs的实现,那么您希望从非环境(删除declare等),最好是外部模块(或者,命名空间)中导出它。 )。

你应该看一下Polymer项目(例如polymer-cli ) - 他们使用带有Typescript的命令行参数。

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

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