Knockout-Validation icon indicating copy to clipboard operation
Knockout-Validation copied to clipboard

Add typescript types to knockout validation

Open SomaticIT opened this issue 5 years ago • 11 comments

Since knockout 3.5 is now publishing Typescript types in its own repository, I suggest to do the same here in knockout.validation.

This types are using the knockout embedded types as a base and extend them to add knockout.validation features.

Linked issue: https://github.com/knockout/knockout/issues/2353

SomaticIT avatar Jun 21 '19 11:06 SomaticIT

Sorry @SomaticIT. I got distracted with getting a feature done and I didn't give you all the comments. I added 4 more comments that I missed the first time I provided comments.

gregveres avatar Oct 04 '19 18:10 gregveres

@gregveres, no problem! I improved types by adding two utility types ValidationExtendType and ValidationExtendAccessType which allows to standardized the behavior of extenders.

SomaticIT avatar Oct 06 '19 17:10 SomaticIT

@SomaticIT @gregveres Any news on merging this PR? :)

sevaru avatar Jul 10 '20 07:07 sevaru

@SomaticIT @gregveres please please please merge it!

sashaostr avatar Dec 04 '20 20:12 sashaostr

Would love to see this merge

SomaticIT avatar Dec 29 '20 15:12 SomaticIT

Hi, what's the status of this pull? I took the knockout.validation.d.ts file from these commits but found I had to make some small type parameter changes to make it work with the latest knockout code

Extender now has a <T> parameter SubscribableFunctions does not have any <T> anymore ObservableExtenderOptions now has a <T> parameter

jeremycrippsbf avatar Nov 16 '21 15:11 jeremycrippsbf

@jeremycrippsbf, do not hesitate to create a patch file, I will integrate it in this PR and give some advices to help users waiting for this PR.

SomaticIT avatar Nov 17 '21 13:11 SomaticIT

I've seen two of the points made by @jeremycrippsbf but don't see the SubscribableFunctions not having a parameter in the current KO source. I also had to add another parent class to ObservableExtenderOptions to lose the compiler error.

Patch below:

diff --git a/dist/knockout.validation.d.ts b/dist/knockout.validation.d.ts
index 5f34394..18e6e00 100644
--- a/dist/knockout.validation.d.ts
+++ b/dist/knockout.validation.d.ts
@@ -394,7 +394,7 @@ declare module "knockout" {
         };
     }
 
-    export interface Extenders {
+    export interface Extenders<T> {
         /**
          * This is for creating custom validation logic on the fly.
          * 
@@ -444,7 +444,7 @@ declare module "knockout" {
         extend(requestedExtenders: validation.ValidationExtendOptions): this & validation.ObservableValidationExtension;
     }
 
-    export interface ObservableExtenderOptions extends validation.ValidationExtendOptions { }
+    export interface ObservableExtenderOptions<T> extends Partial<ExtendersOptions>, validation.ValidationExtendOptions { }
 }
 
 export = ko.validation;

richardlawley avatar Dec 01 '21 13:12 richardlawley

Hello,

I fixed the knockout validation types based on knockout 3.5.1. It seems to work well in my simple tests.

Can you confirm?

SomaticIT avatar Dec 02 '21 18:12 SomaticIT

Hi,

Looks good, works in my project when included directly (as does your related PR in knockout.mapping).

Because you don't seem to get much traction getting these PRs merged, might I suggest making equivalent PRs into DefinitelyTyped, so people can make use of these before the library authors respond? If they do merge your PR then the DT definition for that library can be removed, but until that point these become a lot more useful :)

richardlawley avatar Dec 13 '21 08:12 richardlawley

Found another problem (I'm currently converting a large codebase!). The message property of a rule can be a string, or a function. The function part isn't shown in the examples, but can be seen here.

I added:

export type ValidationMessageType = string | ((params: any[], observable) => string);

then used this as the type of message on ValidationRuleDefinition and ValidationAsyncRuleDefinition to fix this.

richardlawley avatar Dec 14 '21 10:12 richardlawley