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

I am trying to convert an app.js application to app.ts and the only thing that is missing is the .d file for jquery.unobtrusive-ajax.js and / or jquery.validate.unobtrusive.js.

When I create and compile with tsc, it works fine, but WebEssentials in VS2012 complains:

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

Error:

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

thanks

+4
source share
3 answers

If you do not have type information, and the compiler cannot infer the type, then a problem will arise.

Although you get output from the command line, I still expect it to throw an error. Web Essentials reinforces the situation more, which is the idea of ​​tools.

If you cannot get type information (check specifically typed on GitHub), you can fix the error using a simplified definition ...

 interface JQueryStatic { validator: any; } 

If you have a definition for a validator, you can use this trick to declare it unobtrusive, which will only have missing type information, not the entire validator.

This will lead to loss of input on the validator, but will correct the error.

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

+4
source

Just to illustrate what I think Steve already said, if you download d.ts from DefinitelyTyped and open the file, you will see the Validator interface (among other definitions). It can simply be expanded:

 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 already done for you, elsewhere in the file. You won't get any typed kindness in an unobtrusive element, but at least the rest of the Validator code will be type checked.

+4
source

I solved this by doing any:

 (<any>$.validator).unobtrusive.parse($form); 

I like typescript, but using this is hampered by the lack of definition files. I think the small thing is - tsc works fine, but if Visual Studio is going to complain about simple things like this, this will be a problem for many people.

0
source

All Articles