Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issueHigh Priority
Milestone
Description
TypeScript Version: 3.6.2
Search Terms: type inference 3.6
Code
function asObservable(input: string | ObservableInput<string>): Observable<string> {
return typeof input === 'string' ? of(input) : from(input)
}Expected behavior:
This function managed to compile correctly in 3.5 with RxJS 6.5.2
Actual behavior:
In 3.6, it fails with
Type 'Observable<unknown>' is not assignable to type 'Observable<string>'.
Type 'unknown' is not assignable to type 'string'.ts(2322)
the type of from() does not get inferred anymore.
It is declared as
function from<O extends ObservableInput<any>>(input: O): Observable<ObservedValueOf<O>>Specifically, the ObservedValueOf<T> type fails now with ObservableInput<T>:
type ObservedValueOf<O> = O extends ObservableInput<infer T> ? T : never;
type ObservableInput<T> = SubscribableOrPromise<T> | ArrayLike<T> | Iterable<T>;which worked before.
Related Issues: Filed at ReactiveX/rxjs#4992
Reacted by Ben Lesh and Marshall Bowers
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issueHigh Priority
Activity
j-oliveras commentedon Aug 29, 2019
Related to #33125?
benlesh commentedon Aug 29, 2019
Yeah, this one is really not good for RxJS and will break a lot of people. This comment is a solid illustration of why
maxdeviant commentedon Aug 29, 2019
Not using RxJS, but I can confirm that a number of spots in our codebase where type inference was working before (in 3.4.1) broke after upgrading to 3.6.2.
Fall back to TypeScript 3.5
weswigham commentedon Aug 29, 2019
So, this is my minimal repro:
The
Subscribable<never>is ultimately what produces theunknowninference result, looks like. IfObservableInputonly contains aSubscribable<T>, there's no problem. I'll look into why our inference isunknownnow~weswigham commentedon Aug 29, 2019
Haaaaaaaaaaaaaaaaaaaaaah the issue is with our new inference matching code. We try to infer from
Subscribable<never>toSubscribable<T> | Subscribable<never>- we matchSubscribable<never>up to itself, strip it, then say "welp, nothing left in the source, must be done", and do not produce an inference forT. Now, since there's no inference forT, we assign itunknown, which then is union'ed with the inference from the other input type options once that flows out of the conditional and effectively erases 'em.fatcerberus commentedon Aug 30, 2019
This actually makes perfect sense to me. Since
Subscribable<never>matches itself,Tis effectively a phantom type in that context. Like if you havetype Maybe<T> = [ 'nil' ] | [ 'just', T ], then in the case ofnilyou can use it for anyMaybe<T>you want becauseTisn't used in the resulting structure.I guess it's not too nice if it breaks real-world code though. 😉
fatcerberus commentedon Aug 30, 2019
While the above is true, I did subsequently realize it probably makes the internal union order observable in unexpected ways, which isn't too nice from a theoretical basis either*. So 👍 to #33154 from me.
* Always remember to put down the hood when done working on the car lest people start hot-wiring stuff after you walk away. 🚗 😛
benlesh commentedon Sep 1, 2019
Here's another one that is broken:
In 3.6, this is now
Observable<never>DanielRosenwasser commentedon Sep 9, 2019
Can you all try out
typescript@3.6.3-insiders.20190909on npm?simeyla commentedon Dec 27, 2019
I've seen some cases where VSCode has chosen to put something like :
instead of
I've seen this cause similar problems with inference - that sometimes work and sometimes don't.
So check your imports aren't absolute. (And yes this is a custom operator)