-
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
fix(startWith): add empty signature #4034
Conversation
Hey, Tim. Thanks for the PR. One of the things I'd like to do - now that we have the dtslint infrastructure in place - is to ensure that changes to the typings are accompanied by dtslint tests, so that we can build out the testing of the package's typings. Would you be able to add a few dtslint tests? Basically, we'd want one to ensure that an observable of the expected type is effected. And another to ensure that an error is effected if the value passed to For an example, have a look at And to run the tests, use this command: npm run dtslint |
I totally missed those, thanks for pointing them out Nicholas 😅 . |
Something like this this, right? |
Yeah, that's great. If you want, you can add a test for each signature, but as all of the value types are of type If you want to add another, maybe you could add one to show that a scheduler can be passed as the last parameter? |
a9dee5e
to
4e73844
Compare
4e73844
to
4416884
Compare
}); | ||
|
||
it('should enforce types with one value', () => { | ||
const o = of(1, 2, 3).pipe(startWith('foo')); // $ExpectError |
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.
I feel like this should be // $ExpectType Observable<number|string>
}); | ||
|
||
it('should enforce types with multiple value', () => { | ||
const o = of(1, 2, 3).pipe(startWith(4, 'foo')); // $ExpectError |
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.
Same as above, although this might not be possible in this case without the overloads getting out of control.
src/internal/operators/startWith.ts
Outdated
@@ -7,6 +7,7 @@ import { isScheduler } from '../util/isScheduler'; | |||
import { MonoTypeOperatorFunction, SchedulerLike } from '../types'; | |||
|
|||
/* tslint:disable:max-line-length */ | |||
export function startWith<T>(scheduler?: SchedulerLike): MonoTypeOperatorFunction<T>; |
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.
I think we're going to need to differentiate between what's being passed into the startWith
and what the source observable is to really get this right. 🤔
export function startWith<T, R>(...args: R[], scheduler?: SchedulerLike): OperatorFunction<T, T|R>;
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.
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.
AFAICT this won't be work because it's only possible to define a rest parameter as the last argument in a function.
I think it should be doable if we solve it the same way as in the commit @cartant mentioned.
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.
@timdeschryver You'd need to use a union type as the rest parameter type. Have a look at concat
.
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.
I just noticed that there is already a rest parameters signature. I think the T
in all of the signatures should be replaced with an R
- where R
defaults to T
, as in the referenced commit. And the return type should be OperatorFunction<T, T | R>
which will collapse to OperatorFunction<T, T>
when R
is T
.
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.
Yea, I agree that's what I have right now.
The only difference is that I have a type for each argument, I'll commit and push so you can take a look and give your opinions on it.
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.
Yeah, I guess if R
doesn't have to be T
, having one type parameter per parameter makes sense.
@@ -49,7 +49,7 @@ export function startWith<T>(...array: Array<T | SchedulerLike>): MonoTypeOperat | |||
* @method startWith | |||
* @owner Observable | |||
*/ | |||
export function startWith<T>(...array: Array<T | SchedulerLike>): MonoTypeOperatorFunction<T> { | |||
export function startWith<T, D>(...array: Array<T | SchedulerLike>): OperatorFunction<T, T | D> { |
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.
I think the rest parameter signature should be made the same as the implementation signature. And I think both should be:
export function startWith<T, D = T>(...array: Array<D | SchedulerLike>): OperatorFunction<T, T | D>
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.
Yea I'll add it, but I didn't had the time to adjust and test it. Currently a bit busy, but I'll add it soon.
2a8224a
to
a852868
Compare
compat/operator/startWith.ts
Outdated
@@ -26,5 +26,5 @@ export function startWith<T>(this: Observable<T>, ...array: Array<T | SchedulerL | |||
* @owner Observable | |||
*/ | |||
export function startWith<T>(this: Observable<T>, ...array: Array<T | SchedulerLike>): Observable<T> { | |||
return higherOrder(...array)(this); | |||
return higherOrder<T, T>(...array)(this); |
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.
Is this OK 😅 ?
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.
I seem to always forget the compat
signatures. They should be updated, too.
I need to make myself a checklist of things to do upon making a typings change.
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.
Done, thanks for your help!
6c727cd
to
3ad2cff
Compare
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
I love that this finally allows starting with a different type than values.pipe(
switchMap(value =>
fetchSuggestions(value).pipe(
startWith('LOADING')
catchError(err => [err])
)
)
) Which should be |
Description:
This PR adds an overload to
startWith
that only takes a scheduler.Related issue (if exists):
Closes #3947