-
-
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
Changes from 4 commits
5d9f202
d01ed3a
d5a7a44
0204f6e
b5f8fd2
1a17bee
4a197ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
interface ClearablePromise<T> extends Promise<T> { | ||
/** | ||
* Clears the delay and settles the promise. | ||
* | ||
* @memberof ClearablePromise | ||
*/ | ||
clear(): void; | ||
} | ||
|
||
declare const delay: { | ||
/** | ||
* Create a promise which resolves after the specified `milliseconds`. | ||
* 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
* @returns {ClearablePromise<T>} a promise which resolves after the specified `milliseconds` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
*/ | ||
<T = never>(milliseconds: number, value?: T): ClearablePromise<T>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I still don't get the difference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It is just another way of making it optional.
The one is returning const result = await delay<string>(2000); And TypeScript will think that 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 commentThe reason will be displayed to describe this comment to others. Learn more. Now I see your point, thanks. |
||
|
||
/** | ||
* 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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. According to TSDoc docs, there should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, should be removed. |
||
* @param {*} [reason] Value to reject in the returned promise. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume |
||
* @returns {ClearablePromise<never>} a promise which rejects after the specified `milliseconds`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
*/ | ||
// TODO: Allow providing reason type after https://github.com/Microsoft/TypeScript/issues/5413 will be resolved. | ||
reject(milliseconds: number, reason?: any): ClearablePromise<never>; | ||
}; | ||
|
||
export default delay; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,5 +20,7 @@ const createDelay = willResolve => (ms, value) => { | |
return delayPromise; | ||
}; | ||
|
||
module.exports = createDelay(true); | ||
module.exports.reject = createDelay(false); | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. wow such recursive |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,8 @@ | |
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"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'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.