Declaring a JS library for use with TypeScript

廉价感情. 提交于 2019-12-30 08:20:09

问题


There are many threads for similar issues, but as far as I can tell this one is unique.

I'm using jQuery Address plugin in my app and would like to use it in a TypeScript file. Unfortunately there is no DefinitelyTyped script available for the library. When I try to use jQuery.address, I get:

The property 'address' does not exist on value of type 'jQueryStatic'

Per this thread, I've tried to define address inside of jquery.d.ts:

interface JQueryStatic {
    address(options): any;
    ...
}

And I think this seems to work for $.address(); but not for any of address' methods. I've also tried to create my own .d.ts file per this thread, but still no luck. And I tried using declare in a .d.ts file. No luck.

The only method that I need to use is the parameter method...

$.address.parameter('param', 1);

In which case I get:

The property 'parameter' does not exist on value of type 'address'

Any ideas on how I can resolve this?

edit: I'm working in a Visual Studio C# .net environment, if that helps.


回答1:


You should not need to edit jquery.d.ts itself; put these definitions in their own file so they can be maintained properly. Something minimal would be like this:

// For methods on e.g. $('a')
interface JQuery {
    address(callback?: () => void): JQuery;
}

// For methods on $
interface JQueryStatic {
    address: JQueryAddressStatic;
}

interface JQueryAddressStatic {
    (): JQuery;
    parameter(name: string): string;
    parameter(name: string, value: string, append?: boolean): JQuery;
}



回答2:


The following goes inside jquery.d.ts:

interface JQueryStatic {
    address(options): JQueryAddress;
}

interface JQueryAddress {
    parameter(name, value): any;
}

Hope that helps!



来源:https://stackoverflow.com/questions/18113589/declaring-a-js-library-for-use-with-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!