简体   繁体   English

分解类的打字稿声明

[英]Typescript declaration of decomposed class

I have a I am trying to create a typing for a node module. 我正在尝试为节点模块创建类型。 I found that when I am using decomposed class, the code is not compiling. 我发现当我使用分解的类时,代码没有编译。

This is my declaration file demo.d.ts 这是我的声明文件demo.d.ts

declare namespace DemoNs {
  interface Foo_static {
    new(): Foo_instance;
  }

  interface Foo_instance {}

  export var Foo: Foo_static;
}


declare module 'demo-ns' {
  export = DemoNs
}

and this is my test file demo-tests.ts 这是我的测试文件demo-tests.ts

/// <reference path="demo.d.ts" />

import { Foo } from 'demo-ns';

var a: Foo;

When trying to compile last line of the test file says: 尝试编译测试文件的最后一行时,说:

error TS2304: Cannot find name 'Foo'.

Found the problem. 找到了问题。

The compiler throw the error because even tough I exported Foo, there is no place saying it's a type. 编译器抛出错误,因为即使我很难导出Foo,也没有地方说它是类型。 And therefore he isn't finding Foo. 因此,他没有找到Foo。

I fixed by adding to my namespace Foo in my declaration file, the line: 我通过在声明文件中的行Foo中添加以下行来进行修复:

export type Foo = Foo_instance;

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

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