fix(typescript): tighten TypeScript typings and modernize project tooling configuration

This commit is contained in:
2026-05-01 16:30:12 +00:00
parent dc53984a77
commit d8b0da1826
13 changed files with 3964 additions and 5732 deletions
+1 -1
View File
@@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartpromise',
version: '4.2.3',
version: '4.2.4',
description: 'A TypeScript library for managing promises and Deferred constructs, simplifying asynchronous programming.'
}
+18 -24
View File
@@ -3,23 +3,24 @@ import { defer } from './smartpromise.classes.deferred.js';
export * from './smartpromise.classes.cumulativedeferred.js';
export * from './smartpromise.classes.deferred.js';
export function resolvedPromise(): Promise<void>;
export function resolvedPromise<T>(value: T): Promise<T>;
/**
* Creates a new resolved promise for the provided value.
*/
export const resolvedPromise = <T>(value?: T): Promise<T> => {
export function resolvedPromise<T>(value?: T): Promise<T | void> {
return Promise.resolve(value);
};
}
/**
* Creates a new rejected promise for the provided reason.
*/
export const rejectedPromise = (err) => {
export const rejectedPromise = (err: unknown): Promise<never> => {
return Promise.reject(err);
};
interface IAsyncFunction<T> {
(someArg: T): Promise<T>;
interface IAsyncFunction<TInput, TOutput> {
(someArg: TInput): Promise<TOutput>;
}
/**
@@ -28,26 +29,19 @@ interface IAsyncFunction<T> {
* @param inputArg
* @param functionArg
*/
export const map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
const promiseArray: Promise<any>[] = [];
const resultArray = [];
for (const item of inputArg) {
const promise: Promise<any> = functionArg(item);
promiseArray.push(promise);
promise.then((x) => {
resultArray.push(x);
});
}
await Promise.all(promiseArray);
return resultArray;
export const map = async <TInput, TOutput = TInput>(
inputArg: TInput[],
functionArg: IAsyncFunction<TInput, TOutput>
): Promise<TOutput[]> => {
return Promise.all(inputArg.map(functionArg));
};
export const timeoutWrap = async <T = any>(
export const timeoutWrap = async <T = unknown>(
promiseArg: Promise<T>,
timeoutInMsArg: number,
rejectArg = true
) => {
return new Promise<T>((resolve, reject) => {
): Promise<T | null> => {
return new Promise<T | null>((resolve, reject) => {
setTimeout(() => {
if (rejectArg) {
reject(new Error('timeout'));
@@ -59,14 +53,14 @@ export const timeoutWrap = async <T = any>(
});
};
export const timeoutAndContinue = async <T = any>(
export const timeoutAndContinue = async <T = unknown>(
promiseArg: Promise<T>,
timeoutInMsArg = 60000
) => {
): Promise<T | null> => {
return timeoutWrap(promiseArg, timeoutInMsArg, false);
};
export const getFirstTrueOrFalse = async (promisesArg: Promise<boolean>[]) => {
export const getFirstTrueOrFalse = async (promisesArg: Promise<boolean>[]): Promise<boolean> => {
const done = defer<boolean>();
for (const promiseArg of promisesArg) {
promiseArg.then((resultArg) => {
@@ -1,8 +1,8 @@
import { defer } from "./smartpromise.classes.deferred.js";
import { defer } from './smartpromise.classes.deferred.js';
export class CumulativeDeferred {
private accumulatedPromises: Promise<any>[] = [];
private deferred = defer();
private accumulatedPromises: Promise<unknown>[] = [];
private deferred = defer<void>();
public promise = this.deferred.promise;
constructor() {
@@ -16,17 +16,16 @@ export class CumulativeDeferred {
}
public subDefer() {
const done = defer();
const done = defer<void>();
this.addPromise(done.promise);
return done;
}
public addPromise(promiseArg: Promise<any>) {
public addPromise(promiseArg: Promise<unknown>): void {
this.accumulatedPromises.push(promiseArg);
}
}
export const cumulativeDefer = () => {
return new CumulativeDeferred();
}
};
+11 -11
View File
@@ -3,16 +3,16 @@ export interface IResolve<T> {
}
export interface IReject {
(reason?: any): void;
(reason?: unknown): void;
}
export type TDeferredStatus = 'pending' | 'fulfilled' | 'rejected';
export class Deferred<T> {
public promise: Promise<T>;
public resolve: IResolve<T>;
public reject: IReject;
public status: TDeferredStatus;
public resolve!: IResolve<T>;
public reject!: IReject;
public status!: TDeferredStatus;
public claimed = false;
public claim() {
if (this.claimed) {
@@ -21,8 +21,8 @@ export class Deferred<T> {
this.claimed = true;
}
public startedAt: number;
public stoppedAt: number;
public startedAt!: number;
public stoppedAt!: number;
public get duration(): number {
if (this.stoppedAt) {
return this.stoppedAt - this.startedAt;
@@ -33,12 +33,12 @@ export class Deferred<T> {
constructor() {
this.promise = new Promise<T>((resolve, reject) => {
this.resolve = (valueArg: T | PromiseLike<T>) => {
this.resolve = (valueArg?: T | PromiseLike<T>) => {
this.status = 'fulfilled';
this.stoppedAt = Date.now();
resolve(valueArg);
resolve(valueArg as T | PromiseLike<T>);
};
this.reject = (reason: any) => {
this.reject = (reason?: unknown) => {
this.status = 'rejected';
this.stoppedAt = Date.now();
reject(reason);
@@ -49,6 +49,6 @@ export class Deferred<T> {
}
}
export const defer = <T>() => {
export const defer = <T = void>() => {
return new Deferred<T>();
};
};