Moh*_*uur 6 typescript visual-studio-2015 typescript1.8 typescript-typings
我需要帮助尝试了解创建.d.ts文件的正确方法.
让我感动的是有些人使用这种语法:
// lib-a.d.ts
namespace My.Foo.Bar {
interface IFoo {}
interface IBar {}
}
Run Code Online (Sandbox Code Playgroud)
与
// lib-b.d.ts
declare namespace My.Foo.Bar {
interface IFoo {}
interface IBar {}
}
Run Code Online (Sandbox Code Playgroud)
与
// lib-c.d.ts
namespace My.Foo.Bar {
export interface IFoo {}
export interface IBar {}
}
Run Code Online (Sandbox Code Playgroud)
与
// lib-d.d.ts
declare namespace My.Foo.Bar {
export interface IFoo {}
export interface IBar {}
}
Run Code Online (Sandbox Code Playgroud)
与
// lib-e.d.ts
declare module My.Foo.Bar {
export interface IFoo {}
export interface IBar {}
}
Run Code Online (Sandbox Code Playgroud)
哪一个是正确的?什么是声明用于?什么是出口用于?何时使用命名空间与模块?
正确的做法是:
declare namespace NS {
interface InterfaceTest {
myProp: string;
}
class Test implements InterfaceTest {
myProp: string;
myFunction(): string;
}
}
Run Code Online (Sandbox Code Playgroud)
.ts您始终可以通过编写一些文件并使用--declaration选项 ( ) 进行编译来检查签名是否正确tsc test.ts --declaration。这将生成一个d.ts具有正确类型的文件。
例如,上面的声明文件是由以下代码生成的:
namespace NS {
export interface InterfaceTest {
myProp: string;
}
export class Test implements InterfaceTest {
public myProp: string = 'yay';
public myFunction() {
return this.myProp;
}
}
class PrivateTest implements InterfaceTest {
public myPrivateProp: string = 'yay';
public myPrivateFunction() {
return this.myProp;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1500 次 |
| 最近记录: |