My library utilizes a jQuery-like initialization pattern, along with some specific requirements for the types it should accept and return:
function JQueryInitializer ( selector /*: string | INSTANCE_OF_JQUERY*/ ) {
if ( selector.__jquery ) return selector;
}
function initJQuery ( selector/*: string | INSTANCE_OF_JQUERY*/ )/*: INSTANCE_OF_JQUERY*/ {
return new JQueryInitializer( selector );
}
const plugin = initJQuery.fn = initJQuery.prototype = JQueryInitializer.prototype = {
constructor: initJQuery,
__jquery: true
};
plugin.customFunction = function () {};
initJQuery( '*' ).customFunction; // => Function
const $mockObject = { __jquery: true, customFunction() {} };
initJQuery( $mockObject ) === $mockObject; // => true
Although it functions correctly in JavaScript, I am unsure how to properly type it in TypeScript.
Here are the challenges I'm facing:
- I prefer not to create a separate declaration file as is done with jQuery, rather have TypeScript generate it
- The minified outputted JavaScript size after compilation must remain relatively the same, maintaining code efficiency for my library
- In JavaScript, I can return arbitrary values from functions called with
new Foo()
, whereas TypeScript restricts this behavior to only void functions - I need the ability to extend the prototype and ensure a well-defined generated declaration file
I haven't been able to resolve all these issues simultaneously. Any assistance would be greatly appreciated!