-
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
Type inference problem with first and last #3946
Comments
Actually, I think this is a TypeScript bug. If this signature is removed: export function first<T, S extends T>(
predicate: (value: T, index: number, source: Observable<T>) => value is S,
defaultValue?: T
): OperatorFunction<T, S>; The problem goes away, as the return type from However, with the above signature in place, the return type from That doesn't make much sense, as the signature's return type is |
FWIW, here's a minimal repro of the problem: interface Foo<T> {}
interface Bar<T, S> {}
function foo<T, S extends T>(predicate: (value: T) => value is S, defaultValue?: S): Bar<T, S>;
function foo<T>(predicate: (value: T) => boolean, defaultValue?: T): Foo<T>;
function foo<T>(predicate: (value: T) => boolean, defaultValue?: any): Bar<T, any> | Foo<T> {
throw new Error('Unimplemented');
}
const result = foo(x => x === 's', 'd'); // Foo<"d"> If inserted into the RxJS project - say, after the implementation of However, I've been unable to reproduce this in another project, so it's possible that it's a bug that's only effected with a particular combination of TypeScript version and |
Opened a TypeScript issue for the problem: microsoft/TypeScript#25917 |
The weird, compiler-options-dependent behaviour appears to have been fixed in TypeScript 3.0. |
Bug Report
Current Behavior
The changes in PR #3945 exposed a bug with typings for the
first
andlast
operators.With those operators, it's possible to specify a default. And if one is specified, its type is used to infer both the operator's input and output types. And, in doing so, the inferred
MonoTypeOperatorFunction
type can be incompatible with the source observable.Reproduction
See this test for
first
:Expected behavior
Because the default value is specified as using a string literal, the inferred type is a literal type:
MonoTypeOperatorFunction<"d">
and that's not compatible with the source observable's type ofObservable<string>
.Possible Solution
The current workaround used for the test is to explicitly specify the type parameter:
Instead of using
T
as the type for thedefault
parameter, it ought to be possible to use another type parameter that extendsT
. Something like this, maybe:The text was updated successfully, but these errors were encountered: