简体   繁体   English

如何在TypeScript中将静态类添加到声明文件中?

[英]How add a Static Class into a Declaration File in TypeScript?

I'm creating a UMD library in TypeScript . 我正在TypeScript中创建UMD库。 I have my first class, a static one, with a method. 我有第一堂课,一门静态课,有一个方法。 My Library is named SuperLib and this is the code: 我的图书馆名为SuperLib ,这是代码:

export class Security {
  static userExists ( user: string ): boolean {
     ...
  }
}

With Webpack I generate the JS Lib: SuperLib.js and it's working correctly in this way: 使用Webpack,我生成了JS Lib: SuperLib.js ,它以这种方式正常工作:

<head>
  <script src="libs/SuperLib.js"></script>
</head>
<body>
  <script>
    var exits = SuperLib.Security.userExists ("user01");
    console.log ("User01 exits? " + exits)
  </script>

Now I created the Declaration File : SuperLib.d.ts . 现在,我创建了声明文件SuperLib.d.ts I created in this way: 我以这种方式创建的:

declare namespace SuperLib {
  export class Security {
    userExists ( user: string ): boolean;
  }
}

My situation: 我的情况:

Using my library in a HTML, with that Declaration File in WebStorm , the Security class is not treating as a static class. 在HTML中使用我的库,在WebStorm中使用该声明文件 ,则Security类不会被视为静态类。

The autocomplete fills as: 自动填充的内容为:

var exits = SuperLib.Security().userExists ("user01"); << Look the () after security. <<在安全性之后查找()。

I need that shows in this way: 我需要以这种方式显示:

var exits = SuperLib.Security.userExists ("user01");

Why? 为什么? How I can fix it? 我该如何解决?

In the Declaration file is missing 'static'. 在声明文件中缺少“静态”。 The corrected code is: 正确的代码是:

declare namespace SuperLib {
  export class Security {
    static userExists ( user: string ): boolean;
  }
}

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

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