-
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
TypeScript: Pluck has a type safety detonator in it #5188
Comments
Yeah, this could be better. It's inferred as const source = of({ foo: "bar" });
const plucked = source.pipe(pluck("goo")); // Observable<unknown> Because, as you've pointed out, it matches the catch-all, rest parameter signature - but it should effect a compile-time error. The catch-all should include a rest parameter after the other parameters - as in this |
And if you are more specific, it has specific type, not the interface A {
foo: string;
}
const source: Observable<A> = of({ foo: 'bar' });
const plucked: Observable<A['foo']> = source.pipe(pluck('goo')); This |
Looks like this signature works for the catch-all and it definitely returns export declare function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], K5 extends keyof T[K1][K2][K3][K4], K6 extends keyof T[K1][K2][K3][K4][K5]>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6, ...properties: string[]): OperatorFunction<T, unknown>; |
…5192) * fix(pluck): fix pluck's catch-all signature for better type safety Change pluck's catch-all signature that is causing troubles when the passed string is not a key in the argument object. Issue: #5188 * fix(pluck): fix signature and tests Correct the signtaure to be accepted by tests and to return unknown in general * fix(pluck): add test case to check type inference
…eactiveX#5192) * fix(pluck): fix pluck's catch-all signature for better type safety Change pluck's catch-all signature that is causing troubles when the passed string is not a key in the argument object. Issue: ReactiveX#5188 * fix(pluck): fix signature and tests Correct the signtaure to be accepted by tests and to return unknown in general * fix(pluck): add test case to check type inference
Bug Report
Current Behavior with reproduction example
The
pluck
operator is working poorly with some cases in terms of type safety.We use a rig to generate props for React components from Rx Observables. The function uses
Partial
of the Component Props type and expect this type to be returned. In general we have something like this:So, it's pretty fine with that in terms of type safety. Let's assume we have the following in
myStream$
:Type inference for
pluck
will work in an unexpected way. If you look carefully, you'll see that ifsomeField
is absent from the expected object type thankeyof
can't be inferred and we have a general overload that gets someR
from what it is expected to be:The problem in this case is that we have a type for
myProp
field and theR
is inferred from it. So, we have a false expectation that there should beMyPropType
returned frompluck
. But suddenly, there is anundefined
there, in runtime.Expected behavior
It's expected to have any kind of type check error if we pass a key that's absent in the object type we have in the
pluck
input regardless of the expectations.Environment
The text was updated successfully, but these errors were encountered: