Skip to content

Commit de221cd

Browse files
committed
feat(types): NodeErrorConstructor
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 18825fd commit de221cd

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"typecheck:watch": "vitest typecheck"
6565
},
6666
"dependencies": {
67+
"@flex-development/tutils": "6.0.0-alpha.7",
6768
"node-inspect-extracted": "2.0.0"
6869
},
6970
"devDependencies": {
@@ -72,7 +73,6 @@
7273
"@commitlint/types": "17.0.0",
7374
"@faker-js/faker": "7.6.0",
7475
"@flex-development/mkbuild": "1.0.0-alpha.9",
75-
"@flex-development/tutils": "6.0.0-alpha.7",
7676
"@graphql-eslint/eslint-plugin": "3.13.0",
7777
"@types/chai": "4.3.4",
7878
"@types/chai-string": "1.4.2",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @file Unit Tests - NodeErrorConstructor
3+
* @module create-node-error/types/tests/unit-d/NodeErrorConstructor
4+
*/
5+
6+
import type NodeError from '../node-error'
7+
import type TestSubject from '../node-error-constructor'
8+
9+
describe('unit-d:types/NodeErrorConstructor', () => {
10+
it('should extract parameters of type any[] if M extends string', () => {
11+
expectTypeOf<TestSubject>().parameters.toEqualTypeOf<any[]>()
12+
expectTypeOf<TestSubject>().constructorParameters.toEqualTypeOf<any[]>()
13+
})
14+
15+
it('should extract parameters of type Parameters<M> if M extends MessageFn', () => {
16+
type B = TypeErrorConstructor
17+
type M = (ext: string, path: string) => string
18+
type P = Parameters<M>
19+
20+
expectTypeOf<TestSubject<B, M>>().parameters.toEqualTypeOf<P>()
21+
expectTypeOf<TestSubject<B, M>>().constructorParameters.toEqualTypeOf<P>()
22+
})
23+
24+
it('should return NodeError<T>', () => {
25+
expectTypeOf<TestSubject>().returns.toEqualTypeOf<NodeError>()
26+
expectTypeOf<TestSubject<TypeErrorConstructor>>().returns.toEqualTypeOf<
27+
NodeError<TypeError>
28+
>()
29+
})
30+
})

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55

66
export type { default as MessageFn } from './fn-message'
77
export type { default as NodeError } from './node-error'
8+
export type { default as NodeErrorConstructor } from './node-error-constructor'
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @file Type Definitions - NodeErrorConstructor
3+
* @module create-node-error/types/NodeErrorConstructor
4+
*/
5+
6+
import type { Overwrite } from '@flex-development/tutils'
7+
import type MessageFn from './fn-message'
8+
import type NodeError from './node-error'
9+
10+
/**
11+
* Node.js error constructor type.
12+
*
13+
* @template B - Error constructor type
14+
* @template M - Error message type
15+
* @template T - Error base type
16+
*
17+
* @extends {Overwrite<B, B>}
18+
*/
19+
type NodeErrorConstructor<
20+
B extends ErrorConstructor = ErrorConstructor,
21+
M extends MessageFn | string = string,
22+
T extends B['prototype'] = B['prototype']
23+
> = Overwrite<B, B> & {
24+
new (...args: M extends MessageFn ? Parameters<M> : any[]): NodeError<T>
25+
(...args: M extends MessageFn ? Parameters<M> : any[]): NodeError<T>
26+
}
27+
28+
export type { NodeErrorConstructor as default }

0 commit comments

Comments
 (0)