Skip to content

from() does not infer type correctly with TS 3.6 #4992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
felixfbecker opened this issue Aug 29, 2019 · 9 comments
Closed

from() does not infer type correctly with TS 3.6 #4992

felixfbecker opened this issue Aug 29, 2019 · 9 comments

Comments

@felixfbecker
Copy link
Contributor

felixfbecker commented Aug 29, 2019

Bug Report

Current Behavior

Type 'Observable<unknown>' is not assignable to type 'Observable<string>'.
  Type 'unknown' is not assignable to type 'string'.ts(2322)

Reproduction

function asObservable(input: string | ObservableInput<string>): Observable<string> {
    return typeof input === 'string' ? of(input) : from(input)
}

Expected behavior
Should compile

Environment

  • RxJS version: 6.5.2
  • TS version: 3.6.2

Possible Solution
Instead of using ObservedValueOf<T>:

export declare function from<O extends ObservableInput<any>>(input: O): Observable<ObservedValueOf<O>>

declare from like:

export declare function from<O>(input: ObservableInput<O>): Observable<O>

this seems to work.

Additional context/Screenshots
2019-08-29 10 49 04

@cartant
Copy link
Collaborator

cartant commented Aug 29, 2019

FWIW, I think the problem is that a string is also an ObservableInput<string>. This does not effect the same problem, AFAICT:

function asObservable(input: string | Promise<string>): Observable<string> {
  return typeof input === "string" ? of(input) : from(input);
}

What it is in TypeScript 3.6, that has effected this changed behaviour, IDK.

@felixfbecker
Copy link
Contributor Author

That is the reason we wrote the type switch in that helper, but still you'd expect ObservedValueOf<Promise<string>> to infer string, since Promise<string> should clearly only match the Promise<T> in ObservableInput<T>

@cartant
Copy link
Collaborator

cartant commented Aug 29, 2019

Hard to see this as anything other than a TypeScript bug:

declare const i: ObservableInput<string>;
const r = from(i); // Observable<unknown>

And one that will pretty badly wreck the typings that will be even more widely used in v7. 😭

@cartant
Copy link
Collaborator

cartant commented Aug 29, 2019

@benlesh ^

@felixfbecker
Copy link
Contributor Author

Yeah already filed in microsoft/TypeScript#33131

But is there a need to use ObservedValueOf here?

@cartant
Copy link
Collaborator

cartant commented Aug 29, 2019

But is there a need to use ObservedValueOf here?

I think there might be, yes:

eb1d596

@benlesh
Copy link
Member

benlesh commented Aug 29, 2019

Yes, specifically this doesn't work without it:

// from(Observable<A> | Observable<B>): Observable<A | B>
from(Math.random() > 0.5 ? of('123') : of(123));

@cartant
Copy link
Collaborator

cartant commented Sep 11, 2019

This seems to be fixed in TypeScript 3.6.3.

@felixfbecker
Copy link
Contributor Author

Yep, can confirm it’s fixed.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants