Your question is unclear; however, from your comment "imagine if I have use the plugin that doesn't have .ts file" it seems like you are asking: How can I use a jQuery plugin that doesn't already a .d.ts
definitions file.
The reason jQuery is defined in TypeScript is because are using a jQuery definitions file. That file tells TypeScript that $()
is a function that returns an object with the known jQuery functions.
If you create a new plugin, you should create a .d.ts
file that will let users of your plugin reference your code from TypeScript.
If you use a plugin that doesn't have a TypeScript definition file, you have to create a definition yourself, or you can cast your jQuery object to any
. The first option is much better because the IDE and the compiler will still help you.
Let's say you create a new plugin that replaces the content of the object with the words "Hello World";
(function( $ ) {
$.fn.helloWorld = function() {
this.html('Hello World');
return this;
}
})(jQuery);
// Either the plugin author provides the definition, or you write them yourself
interface JQuery {
helloWorld();
}
TypeScript allows you to define an interface multiple times, combining it with previous definitions, so the above definition won't overwrite the existing definition.
See Using jQuery plugin in TypeScript
And here's an example that shows that you can add behavior to existing interfaces
Definitely Typed is a resource maintains definition files that allow you to use regular JavaScript libraries from your TypeScript and it has a number of definition for popular jQuery plugins