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

async: Various fixes, mostly related to strict-nulls. #15011

Merged
merged 7 commits into from Mar 14, 2017

Conversation

webbiesdk
Copy link
Contributor

Various fixes, mostly related to strict-nulls.
Also some places where the error type was wrong.

I wrote the type for the empty array as undefined[]. At first i wrote never[], but I think a lot of people would get confused by the never.

  • Make your PR against the master branch.
  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run tsc without errors.
  • Run npm run lint package-name if a tslint.json is present. (used it to change a few things, but there is still a lot of warnings (as there was before))

Select one of these and delete the others:

If changing an existing definition:

  • Provide a URL to documentation or source code which provides context for the suggested changes: <> (not relevant)
  • Increase the version number in the header if appropriate. (not appropriate)
  • If you are making substantial changes, consider adding a tslint.json containing { "extends": "../tslint.json" }.

Also some places where the error type was wrong.
@dt-bot
Copy link
Member

dt-bot commented Mar 6, 2017

async/index.d.ts

to authors (@borisyankov @Kern0 @Penryn @fenying @pascalmartin). Could you review this PR?
👍 or 👎?

Checklist

  • pass the Travis CI test?

async/index.d.ts Outdated

times<T, E> (n: number, iterator: AsyncResultIterator<number, T, E>, callback: AsyncResultArrayCallback<T, E>): void;
timesSeries<T, E>(n: number, iterator: AsyncResultIterator<number, T, E>, callback: AsyncResultArrayCallback<T, E>): void;
timesLimit<T, E>(n: number, limit: number, iterator: AsyncResultIterator<number, T, E>, callback: AsyncResultArrayCallback<T, E>): void;

transform<T, R, E>(arr: T[], iteratee: (acc: R[], item: T, key: string, callback: (error?: E) => void) => void): void;
transform<T, R, E>(arr: T[], acc: R[], iteratee: (acc: R[], item: T, key: string, callback: (error?: E) => void) => void): void;
transform<T, R, E>(arr: T[], acc: R[], iteratee: (acc: R[] | null, item: T | undefined[], key?: string, callback?: (error?: E) => void) => void): void;
Copy link
Contributor

Choose a reason for hiding this comment

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

An array of undefineds for "item" seems really odd. Is there a doc reference that shows this is possible?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was trying to type the empty array.
Docs: https://caolan.github.io/async/docs.html#transform

But looking at it again, I misunderstood the code (it was passed the accumulator, and in my case that was always empty), and you are right it isn't the right fix.

Actually there is an inconsistency between the implementation and the documentation (both the .d.ts and the official documentation).
If two arguments are passed to the transform function, it crashes.
I've tried to fix that in the implementation: caolan/async#1381
And I'll wait with updating this, until i get an answer there.

async/index.d.ts Outdated

interface AsyncFunction<T, E> { (callback: (err?: E, result?: T) => void): void; }
interface AsyncFunction<T, E> { (callback: (err?: E, result?: T) => void): any; }
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this any necessary? A function that returns a value is assignable to a function type that returns void, so you can still call the functions below with functions that return a value. void makes it clearer that the return value is not used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought that a function declared as returning void actually had to return void (since it works that way with variables).
Thanks for explaining that, I'll fix it.

nextTick(callback: Function, ...args: any[]): void;
setImmediate: typeof async.nextTick;

reflect<T, E>(fn: AsyncFunction<T, E>) : (callback: (err: void, result: {error?: Error, value?: T}) => void) => void;
reflectAll<T, E>(tasks: AsyncFunction<T, E>[]): ((callback: (err: void, result: {error?: Error, value?: T}) => void) => void)[];
reflect<T, E>(fn: AsyncFunction<T, E>) : (callback: (err: null, result: {error?: E, value?: T}) => void) => void;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is null appropriate here? Are you guaranteed not to get an error from a reflected function? Could you provide a source doc please so I can understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are guaranteed not to get an error from a reflected function: https://caolan.github.io/async/docs.html#reflect

And in the implementation, they way the callback is called is:

reflectCallback(null, {
    value: value
});

Or

reflectCallback(null, {
    error: err
});

That is why i put in the null, because it is allways null.

async/index.d.ts Outdated
reflect<T, E>(fn: AsyncFunction<T, E>) : (callback: (err: void, result: {error?: Error, value?: T}) => void) => void;
reflectAll<T, E>(tasks: AsyncFunction<T, E>[]): ((callback: (err: void, result: {error?: Error, value?: T}) => void) => void)[];
reflect<T, E>(fn: AsyncFunction<T, E>) : (callback: (err: null, result: {error?: E, value?: T}) => void) => void;
reflectAll<T, E>(tasks: AsyncFunction<T, E>[]): ((callback: (err: undefined, result: {error?: E, value?: T}) => void) => void)[];
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question as above, and also why is this undefined and the other one null? Again a doc would be helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually just forgot to change the second one to null (same argumentation as above).

I then later changed it from void to undefined, because it wasn't in a function-return.

I'm changing it to null now.

@minestarks
Copy link
Contributor

Additionally since these are strict null fixes, could you turn on strictNullChecks in the tsconfig.json and update the tests please?

@webbiesdk
Copy link
Contributor Author

I still havn't looked into transform (waiting for the async guys to answer my pull-request).

I fixed the other things, and I found 3 other small errors (8875c23)

@webbiesdk
Copy link
Contributor Author

So the async guys merged my pull request (caolan/async#1381), and I updated async.transform to reflect the actual type that it has.

@minestarks minestarks merged commit 9b3bf64 into DefinitelyTyped:master Mar 14, 2017
@webbiesdk webbiesdk deleted the async-fixes branch March 15, 2017 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants