We are using Knockout.js (v3.5.0) and its TypeScript definitions. They worked OK until TypeScript 4.6.2. However the problem seems to be "deeper" than in the definitions file. It seems that there was some change in TypeScript in handling a boolean type. So rather than tagging this question as Knockout.js problem, I created small example of code inspired by the Knockout d.ts that illustrates the problem:
interface Observable<T>
{
(): T;
(value: T): any;
}
function observable<T>(value: T): Observable<T>
{
return undefined as any; // the implementation is not important
}
const x: Observable<boolean> = observable(false);
This code has one compilation problem:
Type 'Observable<false>' is not assignable to type 'Observable<boolean>'.
Types of parameters 'value' and 'value' are incompatible.
Type 'boolean' is not assignable to type 'false'.
Of course casting the false as boolean works, but I do consider this as hack, not a solution (obviously we need cast on every occurrence of true/false).
Any way to actually solve this?
Edit: based on comment it is obvious that some type checking was changed. More examples can be seen here. Playground Link.
Is there any info (with explanation) for this change?
Edit2: as suggested in comments, I filed a bug report at https://github.com/microsoft/TypeScript/issues/48150
observable(false)does. Are you trying to assigne a boolean to anObservable? If yes, you might useof(false).observable(false). This concept is from Knockout. However the implementation is not important here, nor the interface name. This example should only illustrate the compilation problem. I left the interface name unchanged for better understanding for those who knows knockout.js.const w: Observable<any> = observable(false);works fine, and when I hover over theobservablebit, I seefunction observable<boolean>(value: boolean): Observable<boolean>. So the Playground recognizes thatobservable(false)returns anObservable<boolean>when you writeObservable<any>, but not when you actually writeObservable<boolean>.