Skip to content

Commit 4de8946

Browse files
committed
refactor(ts): allow object types to exclude index signature
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 3f5f7da commit 4de8946

File tree

11 files changed

+68
-36
lines changed

11 files changed

+68
-36
lines changed

src/dtos/__tests__/exception.spec-d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ import type * as aggregate from '@flex-development/aggregate-error-ponyfill'
77
import type {
88
JsonifiableObject,
99
Nilable,
10+
ObjectPlain,
1011
OneOrMany
1112
} from '@flex-development/tutils'
1213
import type TestSubject from '../exception'
1314

1415
describe('unit-d:dtos/ExceptionDTO', () => {
16+
it('should allow T to exclude index signature', () => {
17+
expectTypeOf<TestSubject<Error>>().toMatchTypeOf<TestSubject<ObjectPlain>>()
18+
})
19+
1520
it('should extend aggregate.Options<Cause>', () => {
1621
expectTypeOf<TestSubject>().toMatchTypeOf<aggregate.Options>()
1722
})

src/dtos/exception-http.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
* @module exceptions/dtos/HttpExceptionDTO
44
*/
55

6-
import type { JsonifiableObject, Nullable } from '@flex-development/tutils'
6+
import type {
7+
JsonifiableObject,
8+
Nullable,
9+
ObjectPlain
10+
} from '@flex-development/tutils'
711
import type ExceptionDTO from './exception'
812

913
/**
@@ -16,8 +20,8 @@ import type ExceptionDTO from './exception'
1620
* @extends {ExceptionDTO<T,Cause>}
1721
*/
1822
interface HttpExceptionDTO<
19-
T extends JsonifiableObject = JsonifiableObject,
20-
Cause extends JsonifiableObject = JsonifiableObject,
23+
T extends ObjectPlain = JsonifiableObject,
24+
Cause extends ObjectPlain = JsonifiableObject,
2125
Code extends Nullable<number | string> = string
2226
> extends ExceptionDTO<T, Cause, Code> {}
2327

src/dtos/exception.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
JsonifiableObject,
1111
Nilable,
1212
Nullable,
13+
ObjectPlain,
1314
ObjectUnknown,
1415
OneOrMany
1516
} from '@flex-development/tutils'
@@ -24,8 +25,8 @@ import type {
2425
* @extends {aggregate.Options<Cause>}
2526
*/
2627
interface ExceptionDTO<
27-
T extends JsonifiableObject = JsonifiableObject,
28-
Cause extends JsonifiableObject = JsonifiableObject,
28+
T extends ObjectPlain = JsonifiableObject,
29+
Cause extends ObjectPlain = JsonifiableObject,
2930
Code extends Nullable<number | string> = string
3031
> extends aggregate.Options<Cause> {
3132
[key: string]:

src/exceptions/__tests__/base.exception.spec-d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
*/
55

66
import type AggregateError from '@flex-development/aggregate-error-ponyfill'
7-
import type { JsonifiableObject } from '@flex-development/tutils'
7+
import type { JsonifiableObject, ObjectPlain } from '@flex-development/tutils'
88
import type TestSubject from '../base.exception'
99

1010
describe('unit-d:exceptions/Exception', () => {
11+
it('should allow T to exclude index signature', () => {
12+
expectTypeOf<TestSubject<Error>>().toMatchTypeOf<TestSubject<ObjectPlain>>()
13+
})
14+
1115
it('should extend AggregateError<T, Cause & { code: Code }>', () => {
1216
expectTypeOf<TestSubject>().toMatchTypeOf<
1317
AggregateError<JsonifiableObject, JsonifiableObject & { code: string }>

src/exceptions/base.exception.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
isString,
1414
type JsonifiableObject,
1515
type Nullable,
16+
type ObjectPlain,
1617
type OneOrMany
1718
} from '@flex-development/tutils'
1819
import { get, set, shake } from 'radash'
@@ -29,9 +30,9 @@ import { get, set, shake } from 'radash'
2930
* @extends {AggregateError<T,Cause&{code:Code}>}
3031
*/
3132
class Exception<
32-
T extends JsonifiableObject = JsonifiableObject,
33-
Data extends JsonifiableObject = JsonifiableObject,
34-
Cause extends JsonifiableObject = JsonifiableObject,
33+
T extends ObjectPlain = JsonifiableObject,
34+
Data extends ObjectPlain = JsonifiableObject,
35+
Cause extends ObjectPlain = JsonifiableObject,
3536
Code extends Nullable<number | string> = string
3637
> extends AggregateError<T, Cause & { code: Code }> {
3738
/**
@@ -137,9 +138,9 @@ class Exception<
137138
* {@linkcode Exception} instance
138139
*/
139140
public static is<
140-
T extends JsonifiableObject = JsonifiableObject,
141-
Data extends JsonifiableObject = JsonifiableObject,
142-
Cause extends JsonifiableObject = JsonifiableObject,
141+
T extends ObjectPlain = JsonifiableObject,
142+
Data extends ObjectPlain = JsonifiableObject,
143+
Cause extends ObjectPlain = JsonifiableObject,
143144
Code extends Nullable<number | string> = string
144145
>(value: unknown): value is Exception<T, Data, Cause, Code> {
145146
return value instanceof Exception
@@ -161,9 +162,9 @@ class Exception<
161162
* {@linkcode ExceptionJSON} object
162163
*/
163164
public static isJSON<
164-
T extends JsonifiableObject = JsonifiableObject,
165-
Data extends JsonifiableObject = JsonifiableObject,
166-
Cause extends JsonifiableObject = JsonifiableObject,
165+
T extends ObjectPlain = JsonifiableObject,
166+
Data extends ObjectPlain = JsonifiableObject,
167+
Cause extends ObjectPlain = JsonifiableObject,
167168
Code extends Nullable<number | string> = string
168169
>(value: unknown): value is ExceptionJSON<T, Data, Cause, Code> {
169170
/**

src/exceptions/http.exception.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { HttpExceptionJSON } from '#src/interfaces'
1010
import type {
1111
JsonifiableObject,
1212
Nullable,
13+
ObjectPlain,
1314
OneOrMany
1415
} from '@flex-development/tutils'
1516
import { get, invert, set } from 'radash'
@@ -27,9 +28,9 @@ import Exception from './base.exception'
2728
* @extends {Exception<T,Data,Cause,Code>}
2829
*/
2930
class HttpException<
30-
T extends JsonifiableObject = JsonifiableObject,
31-
Data extends JsonifiableObject = JsonifiableObject,
32-
Cause extends JsonifiableObject = JsonifiableObject,
31+
T extends ObjectPlain = JsonifiableObject,
32+
Data extends ObjectPlain = JsonifiableObject,
33+
Cause extends ObjectPlain = JsonifiableObject,
3334
Code extends Nullable<number | string> = string
3435
> extends Exception<T, Data, Cause, Code> {
3536
/**
@@ -112,9 +113,9 @@ class HttpException<
112113
* {@linkcode HttpException} instance
113114
*/
114115
public static override is<
115-
T extends JsonifiableObject = JsonifiableObject,
116-
Data extends JsonifiableObject = JsonifiableObject,
117-
Cause extends JsonifiableObject = JsonifiableObject,
116+
T extends ObjectPlain = JsonifiableObject,
117+
Data extends ObjectPlain = JsonifiableObject,
118+
Cause extends ObjectPlain = JsonifiableObject,
118119
Code extends Nullable<number | string> = string
119120
>(value: unknown): value is HttpException<T, Data, Cause, Code> {
120121
return value instanceof HttpException
@@ -137,9 +138,9 @@ class HttpException<
137138
* `value` is {@linkcode HttpExceptionJSON} object
138139
*/
139140
public static override isJSON<
140-
T extends JsonifiableObject = JsonifiableObject,
141-
Data extends JsonifiableObject = JsonifiableObject,
142-
Cause extends JsonifiableObject = JsonifiableObject,
141+
T extends ObjectPlain = JsonifiableObject,
142+
Data extends ObjectPlain = JsonifiableObject,
143+
Cause extends ObjectPlain = JsonifiableObject,
143144
Code extends Nullable<number | string> = string
144145
>(value: unknown): value is HttpExceptionJSON<T, Data, Cause, Code> {
145146
return (

src/interfaces/__tests__/exception-json.spec-d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
*/
55

66
import type { Errors } from '#src/types'
7-
import type { JsonifiableObject } from '@flex-development/tutils'
7+
import type { JsonifiableObject, ObjectPlain } from '@flex-development/tutils'
88
import type TestSubject from '../exception-json'
99

1010
describe('unit-d:interfaces/ExceptionJSON', () => {
11+
it('should allow T to exclude index signature', () => {
12+
expectTypeOf<TestSubject<Error>>().toMatchTypeOf<TestSubject<ObjectPlain>>()
13+
})
14+
1115
it('should match [cause: Readonly<Cause & { code: Code }>]', () => {
1216
expectTypeOf<TestSubject>()
1317
.toHaveProperty('cause')

src/interfaces/exception-json-http.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
*/
55

66
import type { ClassName, Status, StatusName } from '#src/enums'
7-
import type { JsonifiableObject, Nullable } from '@flex-development/tutils'
7+
import type {
8+
JsonifiableObject,
9+
Nullable,
10+
ObjectPlain
11+
} from '@flex-development/tutils'
812
import type ExceptionJSON from './exception-json'
913

1014
/**
@@ -18,9 +22,9 @@ import type ExceptionJSON from './exception-json'
1822
* @extends {ExceptionJSON<T,Data,Cause,Code>}
1923
*/
2024
interface HttpExceptionJSON<
21-
T extends JsonifiableObject = JsonifiableObject,
22-
Data extends JsonifiableObject = JsonifiableObject,
23-
Cause extends JsonifiableObject = JsonifiableObject,
25+
T extends ObjectPlain = JsonifiableObject,
26+
Data extends ObjectPlain = JsonifiableObject,
27+
Cause extends ObjectPlain = JsonifiableObject,
2428
Code extends Nullable<number | string> = string
2529
> extends ExceptionJSON<T, Data, Cause, Code> {
2630
/**

src/interfaces/exception-json.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
*/
55

66
import type { Errors } from '#src/types'
7-
import type { JsonifiableObject, Nullable } from '@flex-development/tutils'
7+
import type {
8+
JsonifiableObject,
9+
Nullable,
10+
ObjectPlain
11+
} from '@flex-development/tutils'
812

913
/**
1014
* JSON representation of an `Exception`.
@@ -15,9 +19,9 @@ import type { JsonifiableObject, Nullable } from '@flex-development/tutils'
1519
* @template Code - Exception code type
1620
*/
1721
interface ExceptionJSON<
18-
T extends JsonifiableObject = JsonifiableObject,
19-
Data extends JsonifiableObject = JsonifiableObject,
20-
Cause extends JsonifiableObject = JsonifiableObject,
22+
T extends ObjectPlain = JsonifiableObject,
23+
Data extends ObjectPlain = JsonifiableObject,
24+
Cause extends ObjectPlain = JsonifiableObject,
2125
Code extends Nullable<number | string> = string
2226
> {
2327
/**

src/types/__tests__/errors.spec-d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
* @module exceptions/types/tests/unit-d/Errors
44
*/
55

6-
import type { JsonifiableObject } from '@flex-development/tutils'
6+
import type { JsonifiableObject, ObjectPlain } from '@flex-development/tutils'
77
import type TestSubject from '../errors'
88

99
describe('unit-d:types/Errors', () => {
10+
it('should allow T to exclude index signature', () => {
11+
expectTypeOf<TestSubject<Error>>().toMatchTypeOf<TestSubject<ObjectPlain>>()
12+
})
13+
1014
it('should equal T[]', () => {
1115
expectTypeOf<TestSubject>().toEqualTypeOf<JsonifiableObject[]>()
1216
})

src/types/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
* @module exceptions/types/Errors
44
*/
55

6-
import type { JsonifiableObject } from '@flex-development/tutils'
6+
import type { JsonifiableObject, ObjectPlain } from '@flex-development/tutils'
77

88
/**
99
* A group of related errors.
1010
*
1111
* @template T - Aggregated error type
1212
*/
13-
type Errors<T extends JsonifiableObject = JsonifiableObject> = T[]
13+
type Errors<T extends ObjectPlain = JsonifiableObject> = T[]
1414

1515
export type { Errors as default }

0 commit comments

Comments
 (0)