Our team had been using Knockout.js (v3.5.0) along with its TypeScript definitions without any issues until TypeScript 4.6.2 came along. It seems that the problem goes beyond just the definitions file, possibly due to a change in how TypeScript handles boolean types. To showcase this issue, I created a small code example inspired by the Knockout d.ts:
interface Observable<T>
{
(): T;
(value: T): any;
}
function observable<T>(value: T): Observable<T>
{
return undefined as any; // the actual implementation is not crucial for this demonstration
}
const x: Observable<boolean> = observable(false);
This code snippet encounters a compilation error:
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'.
While casting false
as boolean
may work, it feels more like a workaround than a proper solution (especially considering the need to cast every occurrence of true/false). Are there any better approaches to address this?
Edit: Upon reviewing comments, it appears that there have been changes in type checking. Additional examples can be found here. Playground Link.
Has there been any official information (with explanation) regarding this change?
Edit2: Following suggestions from the comments, a bug report has been filed at https://github.com/microsoft/TypeScript/issues/48150