Search code examples
typescriptsizzle

Is there a way to inline SizzleJS when using TypeScript?


I need to inline Sizzle into a TypeScript class, so I can use it from within the class. I can live with not having a definition file for it and just have it as an "any". However the compiler is raising errors from within the minified Sizzle code if I put it at the top of the file. Is there a way to do this?


Solution

  • My recommendation would be to keep it in a JavaScript file and just add a declaration for the bits you need in your TypeScript file - but you have specifically asked for how you could get it inline in a TypeScript file, so here is the answer to your question...

    You could butcher-in some type annotations and type assertions in order to "get away with it" - although it would be quite a brute...

    I have put the full version online as it is too big to place here, but in essence I have either prefixed an expression with a type assertion:

    var high = <any>"0x" + escaped - 0x10000;
    

    Or added a type annotation:

    var addCombinator: any = function( matcher, combinator, base ) {
    

    To any item where the inferred types get violated. You could of course place a reasonable type annotation rather than any if you prefer.

    Fully annotated Sizzle.