Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery.unobtrusive-ajax.d.ts / jquery.validate.unobtrusive.d.ts anyone?

I'm trying to convert my app.js to app.ts and the only thing missing is a .d file for jquery.unobtrusive-ajax.js and/or jquery.validate.unobtrusive.js.

When I build and compile with tsc it works fine but WebEssentials in VS2012 complains about :

var $form = $('#myform');
$.validator.unobtrusive.parse($form); 

Error:

The property 'unobtrusive' does not exist on value of type 'Validator'

Thanks

like image 415
Jonesie Avatar asked Feb 17 '23 08:02

Jonesie


2 Answers

If you haven't got type information, and the compiler can't infer a type then there will be a problem.

Although you are getting output from the command line I would still expect it to give an error. Web Essentials enforces things more strongly, which is the idea behind the tooling.

If you can't get type information (check Definitely Typed on GitHub) you can fix the error using a simplified definition...

interface JQueryStatic {
    validator: any;
}

If you have a definition for validator, you could use this trick to declare unobtrusive against that, which would then only have missing type information rather than all of validator.

This will lose typing on validator but will fix the error.

Taken from http://www.stevefenton.co.uk/Content/Blog/Date/201301/Blog/Complex-TypeScript-Definitions-Made-Easy/

like image 77
Fenton Avatar answered Feb 20 '23 08:02

Fenton


Just to illustrate what I think Steve has already said, if you download the d.ts from DefinitelyTyped, and open up the file, you will see the Validator interface (among other definitions). It is simple to extend it:

interface Validator {
    format(template: string, ...arguments: string[]): string;
    form(): bool;
    element(element: any): bool;
    resetForm(): void;
    showErrors(errors: any): void;
    numberOfInvalids(): number;
    setDefaults(defaults: ValidationOptions): void;
    addMethod(name: string, method: (value: any, element: any, ...params: any[]) => any, message?: any): void;
    addClassRules(rules: any): void;
    addClassRules(name: string, rules: any): void;

    // Just add the unobtrusive element to this (typed as `any` if you don't have time to do anything more)
    unobtrusive:any; 
}

The rest of the work of extending the JQuery and JQueryStatic interfaces is already done for you, elsewhere in the file. You won't get any typed goodness on the unobtrusive element, but at least all the rest of the Validator code will be type checked.

like image 29
Jude Fisher Avatar answered Feb 20 '23 07:02

Jude Fisher