-
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
Use unknown
instead of any
to catch more mistakes
#5839
Conversation
7ac122e
to
2bc201f
Compare
AFAICT, the problem with some of the other places is that there are some assertions that need to be added within the implementations. For example, in rxjs/src/internal/observable/defer.ts Line 54 in 8ef4fee
So an I'm happy to approve this if you want to mark it as ready for review. Other places can be changed in separate PRs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for the PR.
Alternatively, we could do this, so we don't need the assertion: export function defer<E, R extends ObservableInput<E>>(observableFactory: () => R): Observable<E> {
return new Observable<E>((subscriber) => {
innerFrom(observableFactory()).subscribe(subscriber);
});
} … or: export function defer<E>(observableFactory: () => ObservableInput<E>): Observable<E> {
return new Observable<E>((subscriber) => {
innerFrom(observableFactory()).subscribe(subscriber);
});
} WDYT? |
Okay, so the root of this is that For example:
However, I'm not okay with walking away from |
I disagree. There should be no reason why we cannot use |
Oh. I'm not saying this isn't a valid fix. Just noticing the other problem. |
Yeah. I'm not sure about the solution(s) above for |
Can you elaborate a little? If you could tell me the name of an operator that needs it, I can have a look and see what I come up with.
I'm happy to help! I would like to get this issue fixed for all other operators, but I'm a bit uncomfortable sending a PR which adds a lot of type assertions. It would be good if we could find a way to avoid the need for them. |
The issue is that export declare function merge<Sources extends readonly ObservableInput<unknown>[]>(...sources: Sources): Observable<ObservedValueUnionFromArray<Sources>>; in this PR. AFAICT, there is no possibility of splitting out the element type, as done in the I mentioned to Ben that I was planning on coming back to this once the N-args business is done.
TBH, ATM, I'm more comfortable with the type assertion that I mentioned above than the between-two-type-parameters-constraint that's in the first of the two There is a very good changes that there are a whole bunch of operators that can have |
Regarding rxjs/src/internal/observable/defer.ts Lines 55 to 67 in a0bc359
(That types in that implementation are ... 🙈) It's now a straightforward rxjs/src/internal/observable/defer.ts Line 52 in 2337342
If you want to make that change, that would be fine by me. |
Hmm, we might actually be able to do this. How about this pseudo code? type Observable<T> = { t: T };
declare function merge<Sources extends readonly unknown[]>(
...sources: { [K in keyof Sources]: Observable<Sources[K]> }
): Observable<Sources[number]>;
declare const ob1: Observable<number>;
declare const ob2: Observable<string>;
// Observable<string | number>
const result = merge(ob1, ob2); |
Nice! It works with variadic tuples, too |
Where does this leave us then? Maybe we could update all operators to do something like my 2nd export function defer<E>(observableFactory: () => ObservableInput<E>): Observable<E> {
return new Observable<E>((subscriber) => {
innerFrom(observableFactory()).subscribe(subscriber);
});
} |
What we need to do is:
However, I recall now that one of the reasons rxjs/spec-dtslint/observables/defer-spec.ts Lines 15 to 17 in 1e6092e
It's entirely possible that we won't be able to replace it in all situations. Or maybe even at all.
|
FWIW, I've just noticed/remembered that the mechanism you've pointed out was introduced recently. See IMO, we will still need |
Description:
I recently shipped a bug to production which TS should have caught. It looked something like this: https://stackblitz.com/edit/rxjs-jlgx8q?devtoolsheight=60
The reason TS doesn't catch this is because:
mergeMapTo
usesObservableInput<any>
ObservableInput<any>
usesArrayLike<any>
ArrayLike<any>
is assignable to a function Functions are compatible with ArrayLike<any> microsoft/TypeScript#18757We can workaround this by replacing
ObservableInput<any>
withObservableInput<unknown>
, as @cartant suggested on Twitter: https://twitter.com/ncjamieson/status/1318501262162239489.I had a quick go at replacing all usages across the codebase, but I ended up with a lot of failing type tests. Perhaps we will only be able to switch to
unknown
in certain places like this. WDYT?Related issue (if exists):