只是爲了說明我認爲史蒂夫已經說過的話,如果你download the d.ts from DefinitelyTyped,並打開文件,你會看到Validator
接口(以及其他定義)。這是簡單的把它擴大:
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;
}
延長JQuery
和JQueryStatic
接口的其餘工作已經做了你,其他地方的文件。您不會在unobtrusive
元素上獲得任何類型的善意,但至少會對所有其餘的Validator
代碼進行類型檢查。
對不起 - 在我腦海中,我想第二行使用了第一行中選擇的表單!我會更新。 – Fenton 2013-03-02 21:03:26
再次感謝,但我無法讓它工作。它抱怨重複的定義 - 從jquery.validate.d.ts我猜。我以另一種方式解決了它。看到我的答案。乾杯 – Jonesie 2013-03-03 06:30:50
在這種情況下,您可能已經有了jQuery驗證定義,您可以按照我所述進行擴展。如果你通過強制轉換來實現,那麼當你添加一個定義時,你需要使用需要刪除的強制轉換代碼。 – Fenton 2013-03-03 09:03:03