-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support type narrowing via isPromise and default export
- Loading branch information
1 parent
562a060
commit feb90a4
Showing
4 changed files
with
13 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
declare function isPromise(obj: any): boolean; | ||
export = isPromise; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ForbesLindesay
Author
Member
|
||
declare function isPromise<T, S>(obj: Promise<T> | S): obj is Promise<T>; | ||
export default isPromise; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function isPromise(obj) { | ||
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,11 @@ | |
"version": "2.1.0", | ||
"description": "Test whether an object looks like a promises-a+ promise", | ||
"main": "index.js", | ||
"type": "module", | ||
"exports": { | ||
"import": "index.mjs", | ||
This comment has been minimized.
Sorry, something went wrong.
JackStandbridge
|
||
"require": "index.js" | ||
}, | ||
"scripts": { | ||
"test": "mocha -R spec" | ||
}, | ||
|
@@ -16,7 +21,7 @@ | |
"author": "ForbesLindesay", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"better-assert": "~0.1.0", | ||
"mocha": "~1.7.4" | ||
"better-assert": "^1.0.2", | ||
"mocha": "^7.1.1" | ||
} | ||
} |
FYI, as-is, this a major API breaking change to TS consumers. TS has flags (
allowSyntheticDefaultImports
,esModuleInterop
) to allow pulling a cjs module as a default import when the runtime provides that behavior, however if you really wanted to provide a declaration file that simultaneously satisfies both entry points because you're manually patching on a default (you shouldn't, it's a bad idea, generally speaking - hell, having esm and cjs in the same package is a bad idea, generally speaking, since it's like you're cramming two packages with distinct APIs into a single namespace), you should probably do so in a backwards compatible way. In this case, that looks more like:which more accurately reflects the new cjs module shape, and is actually backwards compatible. Better yet, you should probably just leave the type definitions on definitely typed, since these types aren't generated from the source and here the types aren't tested in any way (either on their own, or in concert with every other type definition that depends on them).