Skip to content

Commit 34f5e63

Browse files
committed
fix: correct type versions
1 parent 5fdc7fa commit 34f5e63

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

package.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@
2424
"main": "./dist/index.mjs",
2525
"module": "./dist/index.mjs",
2626
"types": "./dist/index.d.ts",
27-
"typesVersions": {
28-
"*": {
29-
"*": [
30-
"./dist/*",
31-
"./dist/index.d.ts"
32-
]
33-
}
34-
},
3527
"files": [
3628
"dist"
3729
],

src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
type IsFunctionReturnTypePromise<T extends (...args: any[]) => any> =
2-
ReturnType<T> extends Promise<infer U> ? true : false
2+
ReturnType<T> extends Promise<infer _U> ? true : false
33

44
type ExactReturnType<T extends (...args: any[]) => any> = T extends (...args: any[]) => infer R ? R : never
55

@@ -17,7 +17,7 @@ class TryResult<T, E extends Error | null> {
1717
}
1818
}
1919

20-
type MutuallyExclusiveTryResult<T, E extends Error | null> = TryResult<T, null> | TryResult<null, E>
20+
export type ResguardResult<T, E extends Error | null> = TryResult<T, null> | TryResult<null, E>
2121

2222
type ErrorClass = new (...args: any[]) => Error
2323

@@ -29,22 +29,22 @@ export function resguard<_T = never, E extends ErrorClass = ErrorClass>(
2929
export function resguard<T, E extends ErrorClass = ErrorClass>(
3030
func: () => Promise<T>,
3131
_errorType?: E,
32-
): Promise<MutuallyExclusiveTryResult<T, InstanceType<E>>>
32+
): Promise<ResguardResult<T, InstanceType<E>>>
3333

3434
export function resguard<T, E extends ErrorClass = ErrorClass>(
3535
func: () => T,
3636
_errorType?: E,
37-
): MutuallyExclusiveTryResult<T, InstanceType<E>>
37+
): ResguardResult<T, InstanceType<E>>
3838

3939
export function resguard<T, E extends ErrorClass = ErrorClass>(
4040
promise: Promise<T>,
4141
_errorType?: E,
42-
): Promise<MutuallyExclusiveTryResult<T, InstanceType<E>>>
42+
): Promise<ResguardResult<T, InstanceType<E>>>
4343

4444
export function resguard<T, E extends ErrorClass = ErrorClass>(
4545
promiseOrFunction: (() => (T | Promise<T>)) | Promise<T>,
4646
_errorType?: E,
47-
): MutuallyExclusiveTryResult<T, InstanceType<E>> | Promise<MutuallyExclusiveTryResult<T, InstanceType<E>>> {
47+
): ResguardResult<T, InstanceType<E>> | Promise<ResguardResult<T, InstanceType<E>>> {
4848
try {
4949
if (promiseOrFunction instanceof Promise) {
5050
return promiseOrFunction
@@ -68,7 +68,7 @@ export function resguard<T, E extends ErrorClass = ErrorClass>(
6868
}
6969
}
7070

71-
export function resguardFn<T extends (...args: any[]) => any, E extends ErrorClass = ErrorClass, U = any>(
71+
export function resguardFn<T extends (...args: any[]) => any, E extends ErrorClass = ErrorClass, _U = any>(
7272
func: T,
7373
_errorType?: E,
7474
): (...args: Parameters<T>) =>

test/index.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ describe('should', () => {
6565
expectTypeOf(data).toEqualTypeOf<null>()
6666
})
6767

68+
it('should catch a rejected promise', async () => {
69+
const [[data, error]] = await resguard(async () => {
70+
throw new Error('test')
71+
})
72+
expect(data).toBe(null)
73+
expect(error).toBeInstanceOf(Error)
74+
expect(error?.message).toBe('test')
75+
})
76+
6877
it('supports using a function instead of a promise with overrided error type', async () => {
6978
class CustomError extends Error {
7079
x = 1

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"resolveJsonModule": true,
1111
"skipLibCheck": true,
1212
"noImplicitThis": false,
13-
"skipDefaultLibCheck": true
13+
"skipDefaultLibCheck": true,
14+
"baseUrl": "."
1415
}
1516
}

0 commit comments

Comments
 (0)