You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
exportinterfaceObservableBase<T>{(): T;// Read the current value(newValue: T): void;// Write a new valuevalue: T;}exporttypeObservable<T>=Textendsany[] ? ObservableArray<T> : ObservableBase<T>;interfaceObservableArray<Textendsany[]>extendsObservableBase<T>{/** Array push. */push: (arg: T[number])=>void;}functionmakeObservable<T>(initial: T): Observable<T>{return{value: initial,}asObservable<T>;}constx=true;constobs=makeObservable(x);exportfunctionfoo(z: boolean){obs(z);// write a new value (should be `(boolean) => void`)obs();// read the current value (should be `() => boolean`)}
🙁 Actual behavior
No overload matches this call.
Overload 1 of 3, '(newValue: false): true | void', gave the following error.
Argument of type 'boolean' is not assignable to parameter of type 'false'.
Overload 2 of 3, '(newValue: true): false | void', gave the following error.
Argument of type 'boolean' is not assignable to parameter of type 'true'.
Note that obs has a type of ObservableBase<false> | ObservableBase<true>. This doesn't seem right to me. I made an observable that could accept values of true or false, not an observable that only accepts true or an observable that only accepts false.
I was sure I remember seeing something in the TypeScript documentation that allowed a bit more control over when union types are expanded for generics that would prevent this kind of thing, but I have searched high and low and cannot find anything.
If you change makeObservable to return an ObservableBase<T> instead of an Observable<T> then obs correctly gets a type of ObservableBase<boolean> and everything works. But the discriminated union seems to break everything.
🙂 Expected behavior
The above should not have any type errors.
The text was updated successfully, but these errors were encountered:
Bug Report
🔎 Search Terms
Discriminated unions, generic unions, union expansion, boolean expands to
true | false
🕗 Version & Regression Information
Tested on 4.4.2, nightly and 3.9.10.
Possibly related: #22596, #22630, #30029.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Note that
obs
has a type ofObservableBase<false> | ObservableBase<true>
. This doesn't seem right to me. I made an observable that could accept values of true or false, not an observable that only accepts true or an observable that only accepts false.I was sure I remember seeing something in the TypeScript documentation that allowed a bit more control over when union types are expanded for generics that would prevent this kind of thing, but I have searched high and low and cannot find anything.
If you change makeObservable to return an
ObservableBase<T>
instead of anObservable<T>
thenobs
correctly gets a type ofObservableBase<boolean>
and everything works. But the discriminated union seems to break everything.🙂 Expected behavior
The above should not have any type errors.
The text was updated successfully, but these errors were encountered: