-
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
feat(filter, find, first, last): support type guard predicates that don't requiring casting #2119
Conversation
Change looks fine to me, /cc @david-driscoll |
LGTM |
1 similar comment
LGTM |
…r, find, first, last. Abstract ------------ - This fixes ReactiveX#2163 by reverts commit 922d04e. - So this reverts ReactiveX#2119 sadly. Drawback --------- - We would need specify type parameters to their method explicitly if we'd like to narrow the type via `predicate` as type guarde function. - However, I don't think this is a serious problem because we can avoid this drawback with specifying actual types only. - And this changes makes our typings more similar to [TypeScript's Array methods](https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es5.d.ts#L1086-L1097).
…r, find, first, last. Abstract ------------ - This fixes ReactiveX#2163 by reverts commit 922d04e. - So this reverts ReactiveX#2119 sadly. Drawback --------- - We would need specify type parameters to their method explicitly if we'd like to narrow the type via `predicate` as type guarde function. - However, I don't think this is a serious problem because we can avoid this drawback with specifying actual types only. - And this changes makes our typings more similar to [TypeScript's Array methods](https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es5.d.ts#L1086-L1097).
Having issues with this in TS 2.4 within my Angular codebase. @angular/router exports the following type: export type Event = NavigationStart | NavigationEnd | NavigationCancel | NavigationError |
RoutesRecognized | RouteConfigLoadStart | RouteConfigLoadEnd; Pre 2.4 I was doing: navigations: Observable<NavigationEnd> = this.router.events.filter(event => event instanceof NavigationEnd); But now get:
Which looks to me like it's not understanding my instanceof filter, but it only started giving me trouble in 2.4 which apparently strengthens Generics somewhat: https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#improved-inference-for-generics Update: With some help in the TS Gitter room from NaridaL I've got this workaround: navigations: Observable<NavigationEnd> = this.router.events.filter((event): event is NavigationEnd => event instanceof NavigationEnd); |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Description:
These updates allow the use of type guard predicates to flow narrowed types through Observable operator chains using the
filter
,find
,first
, andlast
operators without requiring casting. Unfortunately, direct use oftypeof x === 'string'
style boolean predicates are not able to be supported yet in this context, even though typescript understands how to use them in other contexts (e.g.if/else
statements and such). However, type guard functions can still reduce a lot of boilerplate casting noise with these new definitions. Examples for each operator appear below.Related issue (if exists):
This enhances the initial attempt to integrate type guards for narrowing types in operator chains in #1936. I had initially asked about a solution to this in microsoft/TypeScript#7657 (comment), but arrived at this solution before there was any response.