Skip to content
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

Merged
merged 4 commits into from
Aug 27, 2018

Conversation

timdeschryver
Copy link
Contributor

Description:
This PR adds an overload to startWith that only takes a scheduler.

Related issue (if exists):
Closes #3947

@coveralls
Copy link

coveralls commented Aug 18, 2018

Coverage Status

Coverage remained the same at 96.783% when pulling 3ad2cff on timdeschryver:pr/empty-startwith into 040ec2f on ReactiveX:master.

@cartant
Copy link
Collaborator

cartant commented Aug 19, 2018

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 startWith is not compatible with the source observable's value.

For an example, have a look at spec-dtslint/operators/concatAll-spec.ts.

And to run the tests, use this command:

npm run dtslint

@timdeschryver
Copy link
Contributor Author

I totally missed those, thanks for pointing them out Nicholas 😅 .
You can expect a commit later today/tomorrow.

@timdeschryver
Copy link
Contributor Author

Something like this this, right?
Should I add a test for each type definition?

@cartant
Copy link
Collaborator

cartant commented Aug 19, 2018

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 T, I don't think it's that big a deal. pipe is different, as the parameters chain together.

If you want to add another, maybe you could add one to show that a scheduler can be passed as the last parameter?

});

it('should enforce types with one value', () => {
const o = of(1, 2, 3).pipe(startWith('foo')); // $ExpectError
Copy link
Member

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
Copy link
Member

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.

@@ -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>;
Copy link
Member

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>;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benlesh Yeah, you are right. This is very similar to the changes that were made regarding the default parameters in first and last - in 04f5f0b - that effect a union type if a non-T default is specified.

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

Copy link
Collaborator

@cartant cartant Aug 21, 2018

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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> {
Copy link
Collaborator

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>

Copy link
Contributor Author

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.

@@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this OK 😅 ?

Copy link
Collaborator

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.

Copy link
Contributor Author

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!

Copy link
Collaborator

@cartant cartant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@felixfbecker
Copy link
Contributor

I love that this finally allows starting with a different type than T. I often need to write code like

values.pipe(
  switchMap(value =>
    fetchSuggestions(value).pipe(
      startWith('LOADING')
      catchError(err => [err])
    )
  )
)

Which should be Observable<'LOADING' | Suggestions | Error>, but it was not possible to do before with startWith (only with concat()).

@benlesh benlesh merged commit b7866a0 into ReactiveX:master Aug 27, 2018
@timdeschryver timdeschryver deleted the pr/empty-startwith branch August 27, 2018 17:59
@lock lock bot locked as resolved and limited conversation to collaborators Sep 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants