Repository navigation
fix: Extend generic signature improvements to safe wrapper functions - #498
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Extends the generic type signature improvements from #497 to the safe() / safeGet*() wrappers to improve type inference and align callback signatures across the “safe” helpers.
Changes:
- Update
safe()to default its return type toReturnType<F>instead ofany. - Align
safeGet,safeGetLazy,safeGetWritableLazy,safeGetDeferred, andsafeGetWritableDeferredto the more flexible callback signature pattern (F | ((...args: Parameters<F>) => T)), and adjust generic defaults. - Fix a typo in the
safeGetWritableLazydocumentation example.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| lib/src/helpers/safe.ts | Tightens safe() typing by defaulting the return type to ReturnType<F>. |
| lib/src/helpers/safe_get.ts | Updates safeGet() generics/callback signature pattern for better inference and consistency. |
| lib/src/helpers/safe_lazy.ts | Applies the same signature pattern to lazy/deferred safe wrappers and fixes a doc typo. |
Comments suppressed due to low confidence (5)
lib/src/helpers/safe_lazy.ts:93
- With
safe()now defaulting toReturnType<F>,result.vis typed asT | undefined(sinceISafeReturn.vis optional). The callback passed togetWritableLazy<T>returnsresult.von the non-error branch, making itT | undefinedand likely breaking type-checking. Fix by updatingISafeReturn/safe()to narrowvwhen no error, or normalize/assert the non-error branch before returning.
export function safeGetWritableLazy<T = boolean, F extends (...args: any[]) => T = (...args: any[]) => T>(cb: F | ((...args: Parameters<F>) => T), defValue: T, argArray?: Parameters<F>): ILazyValue<T> {
return getWritableLazy<T>(() => {
let result = safe(cb, argArray);
return result.e ? defValue : result.v;
});
lib/src/helpers/safe_lazy.ts:137
- With
safe()now defaulting toReturnType<F>,result.vis typed asT | undefined(sinceISafeReturn.vis optional). The callback passed intogetDeferred<T, () => T>returnsresult.von the non-error branch, making itT | undefinedand likely breaking type-checking. Fix by updatingISafeReturn/safe()to narrowvwhen no error, or normalize/assert the non-error branch before returning.
export function safeGetDeferred<T, F extends (...args: any[]) => T = (...args: any[]) => T>(cb: F | ((...args: Parameters<F>) => T), defValue: T, argArray?: Parameters<F>): ICachedValue<T> {
return getDeferred<T, () => T>(() => {
let result = safe(cb, argArray);
return result.e ? defValue : result.v;
});
lib/src/helpers/safe_lazy.ts:185
- With
safe()now defaulting toReturnType<F>,result.vis typed asT | undefined(sinceISafeReturn.vis optional). The callback passed intogetWritableDeferred<T, () => T>returnsresult.von the non-error branch, making itT | undefinedand likely breaking type-checking. Fix by updatingISafeReturn/safe()to narrowvwhen no error, or normalize/assert the non-error branch before returning.
export function safeGetWritableDeferred<T, F extends (...args: any[]) => T = (...args: any[]) => T>(cb: F | ((...args: Parameters<F>) => T), defValue: T, argArray?: Parameters<F>): ICachedValue<T> {
return getWritableDeferred<T, () => T>(() => {
let result = safe(cb, argArray);
return result.e ? defValue : result.v;
});
lib/src/helpers/safe.ts:70
- The new default generic
R = ReturnType<F>makesresult.vin callers typed asReturnType<F> | undefined(becauseISafeReturn.vis optional). Callers likesafeGet*()returnresult.vdirectly, so this change is likely to break TypeScript compilation (returningT | undefinedwhereTis expected). Consider updatingISafeReturnto a discriminated union (value vs error) so thatresult.enarrowsresult.v, or otherwise adjustsafe()/call sites to handle the optionalvsafely.
export function safe<F extends (...args: unknown[]) => R, R = ReturnType<F>>(func: F, argArray?: any[]): ISafeReturn<F, R> {
try {
return {
v: func.apply(this, argArray)
};
} catch (e) {
return { e };
}
lib/src/helpers/safe_lazy.ts:49
- With
safe()now defaulting toReturnType<F>,result.vis typed asT | undefined(sinceISafeReturn.vis optional). The non-error branch returnsresult.v, so the callback passed togetLazy<T>becomes() => T | undefined, which is incompatible withTand likely breaks type-checking. Fix by updatingISafeReturn/safe()to narrowvwhen no error, or normalize/assert the non-error branch before returning.
export function safeGetLazy<T = boolean, F extends (...args: any[]) => T = (...args: any[]) => T>(cb: F | ((...args: Parameters<F>) => T), defValue: T, argArray?: Parameters<F>): ILazyValue<T> {
return getLazy<T>(() => {
let result = safe(cb, argArray);
return result.e ? defValue : result.v;
});
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #498 +/- ##
=======================================
Coverage 98.89% 98.89%
=======================================
Files 108 108
Lines 2981 2981
Branches 645 622 -23
=======================================
Hits 2948 2948
Misses 33 33
🚀 New features to boost your workflow:
|
- Update safe() to use ReturnType<F> instead of any for better type inference - Align safeGet, safeGetLazy, safeGetWritableLazy, safeGetDeferred, and safeGetWritableDeferred with the same flexible callback signature pattern - Change default type parameter from () => T to (...args: any[]) => T - Add union type to callback parameters: cb: F | ((...args: Parameters<F>) => T) - Remove unnecessary default (T = boolean) from safeGet - Fix typo in safeGetWritableLazy documentation Extends the type flexibility improvements from #497 to all safe wrapper functions for consistent API patterns and better type inference across the library.
nev21
force-pushed
the
nev21/GenericSig
branch
from
February 3, 2026 06:01
2eccc53 to
9a89995
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Extends the type flexibility improvements from #497 to all safe wrapper functions for consistent API patterns and better type inference across the library.