-
Notifications
You must be signed in to change notification settings - Fork 3k
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
switchAll, mergeAll etc are not type safe #3841
Comments
The problem seems to have been introduced in 6.2.1 with this commit - which uses the signature that I suggested. Prior to that commit, the snippet would have effected this error:
The commit fixed an error with the pipe<R>(op1: OperatorFunction<T, any>, ...operations: OperatorFunction<any, any>[]): Observable<R>; Such a signature would avoid matching the first parameter with pipe<A, R>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, any>, ...operations: OperatorFunction<any, any>[]): Observable<R>; However, such a signature requires the inclusion of another type parameter. And given that the caller of such a rest-parameter signature will want to explicitly specify I don't know if it's going to be possible to support a rest-parameters signature with which the caller can specify only the result type ( As an aside, this is a regression that would have been caught with an appropriate dtslint test - see #3823. Something like this would have caught it: const input: Observable<number> = of(1, 2, 3);
const output = input.pipe(switchAll()); // $ExpectError |
I don't like the idea of degrading type safety for common cases like this just to support more than That said, would microsoft/TypeScript#24897 eliminate the need for all the overloads? We should definitely try that out |
I've seen the TypeScript PR that you've referenced and looking more closely at it is on my TODO list. However, I suspect that it won't help in the |
My preferred solution is to abandon the only-specifiy- That is, remove this: pipe<R>(
...operations: OperatorFunction<any, any>[]
): Observable<R>; And add this: pipe<A, B, C, D, E, F, G, H, I, R>(
op1: OperatorFunction<T, A>,
op2: OperatorFunction<A, B>,
op3: OperatorFunction<B, C>,
op4: OperatorFunction<C, D>,
op5: OperatorFunction<D, E>,
op6: OperatorFunction<E, F>,
op7: OperatorFunction<F, G>,
op8: OperatorFunction<G, H>,
op9: OperatorFunction<H, I>,
...operations: OperatorFunction<any, any>[]
): Observable<R>; Instead of specifying a single const result = of("T").pipe(
mapTo("A"),
mapTo("B"),
mapTo("C"),
mapTo("D"),
mapTo("E"),
mapTo("F"),
mapTo("G"),
mapTo("H"),
mapTo("I"),
mapTo("R")
) as Observable<"R">; // Otherwise inferred to be Observable<{}> |
Overall, the typing around the Perhaps we can get @ahejlsberg or @mhegazy's attention and this can be solved either by helping us get it right, or with some solution to the problem on the TypeScript side of things. |
Replace the rest parameters overload with a signature that also includes the A-I type parameters. Closes ReactiveX#3823 ReactiveX#3841
Replace the rest parameters overload with a signature that also includes the A-I type parameters. Closes ReactiveX#3841
Replace the rest parameters overload with a signature that also includes the A-I type parameters. Closes ReactiveX#3841
Replace the rest parameters overload with a signature that also includes the A-I type parameters. Closes ReactiveX#3841
Bug Report
Current Behavior
No type error, but runtime error:
Reproduction
Expected behavior
This should give a type error like
number is not assignable to ObservableInput<T>
Environment
Possible Solution
I tried a few things, but it seems like type inference just fails here. TypeScript needs to infer a type based on the usage of the return value here.
Workaround
Use
switchMap(x => x)
instead (switchMap(identity)
also breaks inference)The text was updated successfully, but these errors were encountered: