Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extend for a single file?

I have a site with 10 pages, which share a Typescript file, and also each page has its own specific Typescript file. One page has a JQuery plugin, timepicker, so I extend the JQuery object using:

interface JQuery {
    timepicker(options:any):JQuery;
}

However I don't want any of the other TypeScript files to have timepicker on the JQuery object. How can I extend it just for this file?

It doesn't let me extend JQuery inside a namespace.

Can modules help? I have no need to import or export anything, so I am not sure it is appropriate, or how to use modules.

like image 385
wezten Avatar asked Oct 18 '17 12:10

wezten


1 Answers

You could name the interface something more reflective of what it is—something like:

interface JQueryWithTimePicker extends JQuery {
    timepicker(options: any): JQuery;
}

Then your one file that needs the timepicker can use the JQuery object downcast to JQueryWithTimePicker.

I use this strategy sometimes when I need to add properties to DOM nodes. Something like:

interface MyTreeElement extends HTMLElement
{
    Nodes: TreeNode[];
}
like image 75
Peter Fernandes Avatar answered Oct 22 '22 18:10

Peter Fernandes