-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Conversation
index.d.ts
Outdated
|
||
declare const delay: { | ||
<T = never>(ms: number, value?: T): ClearablePromise<T> | ||
reject(ms: number, reason?: any): ClearablePromise<never> |
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.
Why is this not also 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.
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.
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 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 |
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.
clear(): void
index.d.ts
Outdated
} | ||
|
||
declare const delay: { | ||
<T = never>(ms: number, value?: T): ClearablePromise<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 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", |
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'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 |
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.
Semicolon
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.
Can you use export default delay;
?
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.
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.
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, let's do that then. I think it's much nicer to allow TS consumer to use ES2015 import syntax.
@dmitry-korolev @SamVerschueren Any feedback on https://github.com/sindresorhus/typescript-definition-style-guide ? Is it clear enough? Anything missing? |
Could you add docs to the methods? You can just take the text from the readme. |
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; |
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.
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. |
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 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 |
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'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?
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.
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. |
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 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.
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.
Seems it's not: https://github.com/Microsoft/tsdoc
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.
According to TSDoc docs, there should be -
between the param name and description.
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.
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`. |
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.
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` |
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.
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. |
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.
[value]
, is the []
really necessary? The signature already defines it as optional.
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 []
is a leftover from JSDoc, because there was no way of indicating optionals. But I think they should be removed for TS.
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>; |
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 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>
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 there any reason to do that?
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.
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.
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.
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.
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.
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. |
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.
Yes, should be removed.
This is looking great now 🙌 Thanks for your patience @dmitry-korolev :) |
No description provided.