在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java  HTML/ 請問自己寫了一個(gè)typescript的.ts和.d.ts,如何關(guān)聯(lián)這兩個(gè)文件讓聲

請問自己寫了一個(gè)typescript的.ts和.d.ts,如何關(guān)聯(lián)這兩個(gè)文件讓聲明文件生效?

我寫了一個(gè)test.ts

export default class TestClass2 {
    static defaultName: string = "luck";
    static getName(): any {
        return TestClass2.defaultName;
    }
}

和一個(gè)test.d.ts

declare class TestClass2{
    static defaultName: string // 默認(rèn)名字
    static getName(): any // 獲取名字的方法
}

然后我在main.ts里使用這個(gè)類,并沒有相關(guān)的提示(比如我調(diào)用testclass2.defaultName,并沒有“默認(rèn)名字”的代碼提示)。

這個(gè).d.ts到底是如何使用的?如何讓一個(gè).ts文件和它對應(yīng)的.d.ts文件想關(guān)聯(lián)?

回答
編輯回答
巫婆

已解決:
出現(xiàn)上面問題的原因是沒有弄懂ts類型聲明文件的作用。
如果要在ts文件里引入第三方的js庫(比如Jquery或者自己寫的js模塊),那么該js沒有類型約束,怎么辦?這時(shí)候就可以引入該文件的.d.ts,然后typescript就會(huì)根據(jù)這個(gè)類型申明文件對該js進(jìn)行類型驗(yàn)證。
下面引用一下stackoverflow的一段話:
The "d.ts" file is used to provide typescript type information about an API that's written in JavaScript. The idea is that you're using something like jQuery or underscore, an existing javascript library. You want to consume those from your typescript code.

Rather than rewriting jquery or underscore or whatever in typescript, you can instead write the d.ts file, which contains only the type annotations. Then from your typescript code you get the typescript benefits of static type checking while still using a pure JS library.

2018年6月3日 09:31