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

Add TypeScript definition #31

Merged
merged 7 commits into from
Aug 20, 2018
Merged

Conversation

dmitry-korolev
Copy link
Contributor

No description provided.

index.d.ts Outdated

declare const delay: {
<T = never>(ms: number, value?: T): ClearablePromise<T>
reject(ms: number, reason?: any): ClearablePromise<never>
Copy link
Owner

Choose a reason for hiding this comment

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

Why is this not also 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.

Typescript Promise definition doesn't allow to set an error type. Since the Promise, created by reject will always fail, it should have never type.

Copy link
Owner

Choose a reason for hiding this comment

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

I found the relevant TS issue: microsoft/TypeScript#5413 Can you add a TODO comment about making it more strict when that issue is fixed?

index.d.ts Outdated
@@ -0,0 +1,10 @@
interface ClearablePromise<T> extends Promise<T> {
clear (): void
Copy link
Owner

Choose a reason for hiding this comment

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

clear(): void

index.d.ts Outdated
}

declare const delay: {
<T = never>(ms: number, value?: T): ClearablePromise<T>
Copy link
Owner

Choose a reason for hiding this comment

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

I know it's ms in the docs, but can you use milliseconds here for clarity?

package.json Outdated
@@ -18,6 +18,7 @@
"files": [
"index.js"
],
"typings": "index.d.ts",
Copy link
Owner

Choose a reason for hiding this comment

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

There's is no need to add this since it's named index, but you do need to add it to the files property.

index.d.ts Outdated
reject(ms: number, reason?: any): ClearablePromise<never>
}

export = delay
Copy link
Owner

Choose a reason for hiding this comment

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

Semicolon

Copy link
Owner

Choose a reason for hiding this comment

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

Can you use export default delay;?

Copy link
Contributor

Choose a reason for hiding this comment

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

export default isn't going to work because it's just CJS. So if you would write module.exports.default = delay, we could use export default delay; here as well.

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah, let's do that then. I think it's much nicer to allow TS consumer to use ES2015 import syntax.

@sindresorhus sindresorhus changed the title Add typescript typings Add TypeScript typings Aug 20, 2018
@sindresorhus sindresorhus changed the title Add TypeScript typings Add TypeScript definition Aug 20, 2018
@sindresorhus
Copy link
Owner

@dmitry-korolev @SamVerschueren Any feedback on https://github.com/sindresorhus/typescript-definition-style-guide ? Is it clear enough? Anything missing?

@sindresorhus
Copy link
Owner

Could you add docs to the methods? You can just take the text from the readme.

@SamVerschueren
Copy link
Contributor

The style guide looks good! Let's work out the testing first and then we could add it later as well, would be nice.

const delay = createDelay(true);
delay.reject = createDelay(false);
module.exports = delay;
module.exports.default = delay;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

wow such recursive

index.d.ts Outdated
* Optionally pass a `reason` to reject.
*
* @param {number} milliseconds Milliseconds to delay the promise.
* @param {*} [reason] Value to reject in the returned promise.
Copy link
Owner

Choose a reason for hiding this comment

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

I assume * is a shortcut for any, but isn't it better to stay consistent with the actual typing and use any?

index.d.ts Outdated
/**
* Clears the delay and settles the promise.
*
* @memberof ClearablePromise
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not that familiar with TS documentation, but isn't this moot as it's already clear from the context and TS should be able to infer it itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was generated automatically. I'll remove it.

index.d.ts Outdated
* Create a promise which rejects after the specified `milliseconds`.
* Optionally pass a `reason` to reject.
*
* @param {number} milliseconds Milliseconds to delay the promise.
Copy link
Owner

Choose a reason for hiding this comment

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

Is it required to provide the types here? I feel it's moot as it's already defined in the signature and TS could just get it from there. I don't like needless boilerplate.

Copy link
Owner

Choose a reason for hiding this comment

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

Copy link
Owner

Choose a reason for hiding this comment

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

According to TSDoc docs, there should be - between the param name and description.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, should be removed.

index.d.ts Outdated
*
* @param {number} milliseconds Milliseconds to delay the promise.
* @param {*} [reason] Value to reject in the returned promise.
* @returns {ClearablePromise<never>} a promise which rejects after the specified `milliseconds`.
Copy link
Owner

Choose a reason for hiding this comment

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

a => A

index.d.ts Outdated
*
* @param {number} milliseconds Milliseconds to delay the promise.
* @param {T} [value] Value to resolve in the returned promise.
* @returns {ClearablePromise<T>} a promise which resolves after the specified `milliseconds`
Copy link
Owner

Choose a reason for hiding this comment

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

a => A and end it in a .

index.d.ts Outdated
* Optionally pass a `value` to resolve.
*
* @param {number} milliseconds Milliseconds to delay the promise.
* @param {T} [value] Value to resolve in the returned promise.
Copy link
Owner

Choose a reason for hiding this comment

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

[value], is the [] really necessary? The signature already defines it as optional.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think [] is a leftover from JSDoc, because there was no way of indicating optionals. But I think they should be removed for TS.

@sindresorhus
Copy link
Owner

Sorry for all the nitpick, but I intend to use this one as an example for other submissions, so I want to perfect it ;)

index.d.ts Outdated
* @param {T} [value] Value to resolve in the returned promise.
* @returns {ClearablePromise<T>} a promise which resolves after the specified `milliseconds`
*/
<T = never>(milliseconds: number, value?: T): ClearablePromise<T>;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can write the 2 methods separately. This way you have 2 overrides.

(milliseconds: number): ClearablePromise<void>;
<T = any>(milliseconds: number, value: T): ClearablePromise<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.

Is there any reason to do that?

Copy link
Contributor

@SamVerschueren SamVerschueren Aug 20, 2018

Choose a reason for hiding this comment

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

If you use the two lines, it clearly shows that it has 2 overloads. So you can also show other descriptions per method.

screen shot 2018-08-20 at 12 57 15

screen shot 2018-08-20 at 12 57 21

If you only have one, it is shown like this
screen shot 2018-08-20 at 12 56 58

(Docs are coming from @types/delay, so don't bother naming :))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I still don't get the difference.
As for me, the suggested overload is just another way to make the value argument optional. The return types would still be the same. Descriptions also will be identical, since it has to be explicitly stated that the method accepts the second argument.
Finally, the suggested overload won't affect intellisense or type inference in any way.

Copy link
Contributor

Choose a reason for hiding this comment

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

As for me, the suggested overload is just another way to make the value argument optional.

It is just another way of making it optional.

The return types would still be the same.

The one is returning ClearablePromise<void>, the other is returning ClearablePromise<T>. With the current implementation, you can write the following line of code.

const result = await delay<string>(2000);

And TypeScript will think that result is of type string, which it clearly isn't because there is no value provided.

When using overloads, you won't be able to write that line because it isn't accepted by the compiler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now I see your point, thanks.

index.d.ts Outdated
* Create a promise which rejects after the specified `milliseconds`.
* Optionally pass a `reason` to reject.
*
* @param {number} milliseconds Milliseconds to delay the promise.
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, should be removed.

@sindresorhus sindresorhus merged commit 3e383c6 into sindresorhus:master Aug 20, 2018
@sindresorhus
Copy link
Owner

This is looking great now 🙌 Thanks for your patience @dmitry-korolev :)

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.

3 participants