@js-bits/xpromise
Advanced tools
Comparing version 1.0.11 to 1.0.12
export default ExtendablePromise; | ||
export type Resolve<T> = (value: T | PromiseLike<T>, ...rest: unknown[]) => void; | ||
export type Reject = (reason?: Error) => void; | ||
export type ErrorName<T> = T extends `ExtendablePromise|${infer S}` ? `*|${S}` : never; | ||
/** | ||
@@ -55,6 +54,6 @@ * @template T | ||
declare namespace ExtendablePromise { | ||
const ExecutionError: "*|ExecutionError"; | ||
const InstantiationError: "*|InstantiationError"; | ||
const ExecutionError: "ExecutionError"; | ||
const InstantiationError: "InstantiationError"; | ||
} | ||
import * as UniqueSymbols from '@js-bits/enumerate/types/unique-symbols'; |
38
index.js
@@ -13,12 +13,2 @@ import enumerate from '@js-bits/enumerate'; | ||
const ErrorPrefix = 'ExtendablePromise|'; | ||
const ERRORS = enumerate.ts( | ||
` | ||
InstantiationError | ||
ExecutionError | ||
`, | ||
Prefix(ErrorPrefix) | ||
); | ||
/** | ||
@@ -69,2 +59,3 @@ * @template T | ||
const error = new Error(`Invalid executor type: ${executor === null ? 'null' : typeof executor}`); | ||
// eslint-disable-next-line no-use-before-define | ||
error.name = ERRORS.InstantiationError; | ||
@@ -77,3 +68,3 @@ throw error; | ||
get [Symbol.toStringTag]() { | ||
return 'ExtendablePromise'; | ||
return ExtendablePromise.name; | ||
} | ||
@@ -94,2 +85,3 @@ | ||
error.cause = cause; | ||
// eslint-disable-next-line no-use-before-define | ||
error.name = ERRORS.ExecutionError; | ||
@@ -124,16 +116,22 @@ this.reject(error); | ||
/** | ||
* Remove prefix for typescript to make error names overridable | ||
* @type {import("@js-bits/enumerate/types/types").EnumType<"InstantiationError ExecutionError", StringConstructor>} | ||
*/ | ||
// @ts-expect-error ts(2322) | ||
const ERRORS = enumerate.ts( | ||
` | ||
InstantiationError | ||
ExecutionError | ||
`, | ||
Prefix(`${ExtendablePromise.name}|`) | ||
); | ||
// https://stackoverflow.com/a/60328122 | ||
Object.defineProperty(ExtendablePromise, Symbol.species, { get: () => Promise }); | ||
/** | ||
* @template T | ||
* @typedef {T extends `${ErrorPrefix}${infer S}` ? `*|${S}`: never} ErrorName | ||
*/ | ||
// Assigning properties one by one helps typescript to declare the namespace properly | ||
ExtendablePromise.ExecutionError = /** @type {ErrorName<typeof ERRORS.ExecutionError>} */ (ERRORS.ExecutionError); | ||
ExtendablePromise.InstantiationError = /** @type {ErrorName<typeof ERRORS.InstantiationError>} */ ( | ||
ERRORS.InstantiationError | ||
); | ||
ExtendablePromise.ExecutionError = ERRORS.ExecutionError; | ||
ExtendablePromise.InstantiationError = ERRORS.InstantiationError; | ||
export default ExtendablePromise; |
@@ -52,3 +52,3 @@ /* eslint-disable max-classes-per-file, import/no-extraneous-dependencies, no-unused-vars */ | ||
try { | ||
// @ts-expect-error Argument of type 'number' is not assignable to parameter of type 'Function'. | ||
// @ts-expect-error ts(2345) | ||
promise = new ExtendablePromise(123); | ||
@@ -194,3 +194,3 @@ } catch (error) { | ||
expect.assertions(1); | ||
// @ts-expect-error Expected 1 arguments, but got 3. | ||
// @ts-expect-error ts(2554) | ||
return promise.resolve(11, 22, 33).then((...args) => { | ||
@@ -375,3 +375,3 @@ expect(args).toEqual([11]); | ||
expect.assertions(1); | ||
// @ts-expect-error Expected 0-1 arguments, but got 3. | ||
// @ts-expect-error ts(2554) | ||
const promise = Promise.resolve(1, 2, 3); | ||
@@ -378,0 +378,0 @@ return promise.then((...args) => { |
{ | ||
"name": "@js-bits/xpromise", | ||
"version": "1.0.11", | ||
"version": "1.0.12", | ||
"description": "Extendable Promise", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28368
739