Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript compiler bug? knockout.validation.d.ts doesn't compile anymore

Tags:

typescript

I just updated Typescript from v2.3 to v2.4, and now it is giving me an error on the knockout.validation.d.ts lines:

interface KnockoutSubscribableFunctions<T> {
    isValid: KnockoutComputed<boolean>;
    isValidating: KnockoutObservable<boolean>;
    rules: KnockoutObservableArray<KnockoutValidationRule>;
    isModified: KnockoutObservable<boolean>;
    error: KnockoutComputed<string>;
    setError(error: string): void;
    clearError(): void;
}

Here knockout.validation is trying to indicate that KnockoutSubscribableFunctions now has extra members. Here is the definition of this interface in knockout.d.ts:

interface KnockoutSubscribableFunctions<T> {
    [key: string]: KnockoutBindingHandler;

    notifySubscribers(valueToWrite?: T, event?: string): void;
}

the compiler now complains that:

Property 'isValid' of type 'KnockoutComputed' is not assignable to string index type 'KnockoutBindingHandler'.

I don't understand why it doesn't see these new values as new properties in the interface? why is it trying to say that they have to map onto the index signatures? The docs seem to indicate that you can have the index signature and other properties in the same interface.

I took the initial definition of the interface into the playground and it even complained that notifySubscribers isn't assignable to a KnockoutBindingHandler.

With the new compiler how would you get this code to compile?

For now there I'm using a brute force method to get this to compile, in which I am changing the knockout.d.ts definition to be as follows:

interface KnockoutSubscribableFunctions<T> {
    [key: string]: any;//KnockoutBindingHandler;

    notifySubscribers(valueToWrite?: T, event?: string): void;
}
like image 290
Greg Veres Avatar asked Jul 16 '17 23:07

Greg Veres


1 Answers

The problem exists because of differences in types of the:

[key: string]: KnockoutBindingHandler;

And other params:

isValid: KnockoutComputed<boolean>;
isValidating: KnockoutObservable<boolean>;
rules: KnockoutObservableArray<KnockoutValidationRule>;
isModified: KnockoutObservable<boolean>;
error: KnockoutComputed<string>;
setError(error: string): void;
clearError(): void;

The error you got basically says: the KnockoutComputed type can't be assigned to the KnockoutBindingHandler type.

Probably this compile-time checking are improved in TS 2.4, that's why you hadn't had this problem previously.

Your solution works:

[key: string]: any;//KnockoutBindingHandler;

And if you can change this code you may try another a little bit "prettier" solution:

[key: string]: any | KnockoutBindingHandler;

Which might provide you some additional autocomplete help.

like image 117
Mark Dolbyrev Avatar answered Nov 20 '22 13:11

Mark Dolbyrev