Skip to content

Commit

Permalink
fix(types): [OneOrMany] unionize writable array
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Jul 26, 2023
1 parent 3f3c809 commit 06ba1a6
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 16 deletions.
9 changes: 4 additions & 5 deletions src/types/__tests__/one-or-many.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ describe('unit-d:types/OneOrMany', () => {
expectTypeOf<TestSubject<T>>().extract<T>().not.toBeNever()
})

it('should extract readonly T[]', () => {
expectTypeOf<TestSubject<T>>().extract<readonly T[]>().not.toBeNever()
it('should extract T[]', () => {
expectTypeOf<TestSubject<T>>().extract<T[]>().not.toBeNever()
})

it('should match T[]', () => {
expectTypeOf<T[]>().toMatchTypeOf<TestSubject<T>>()
expectTypeOf<TestSubject<T>>().extract<T[]>().toBeNever()
it('should extract readonly T[]', () => {
expectTypeOf<TestSubject<T>>().extract<readonly T[]>().not.toBeNever()
})
})
2 changes: 1 addition & 1 deletion src/types/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Assigner<T extends Objectify<any>, U> = IsEqual<T, U> extends true
*/
type Assign<
T extends object,
U extends OneOrMany<ObjectCurly> = EmptyObject
U extends Readonly<OneOrMany<ObjectCurly>> = EmptyObject
> = IsAnyOrNever<U> extends true
? Assigner<T, Objectify<U>>
: T extends unknown
Expand Down
2 changes: 1 addition & 1 deletion src/types/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type Defaulter<T extends Objectify<any>, U> = IsEqual<T, U> extends true
*/
type Defaults<
T extends object,
U extends OneOrMany<ObjectCurly> = EmptyObject
U extends Readonly<OneOrMany<ObjectCurly>> = EmptyObject
> = IsAnyOrNever<U> extends true
? Defaulter<T, Objectify<U>>
: T extends unknown
Expand Down
2 changes: 1 addition & 1 deletion src/types/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type Merger<
*/
type Merge<
T extends object,
U extends OneOrMany<ObjectCurly> = EmptyObject,
U extends Readonly<OneOrMany<ObjectCurly>> = EmptyObject,
M extends EmptyObject & { concat?: Nilable<boolean> } = EmptyObject
> = IsAnyOrNever<U> extends true
? Merger<T, Objectify<U>>
Expand Down
6 changes: 3 additions & 3 deletions src/types/one-or-many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
*/

/**
* Constructs a union of `T` and an array of `T` values.
* Constructs a union of `T` and a writable array of `T` values.
*
* @example
* type X = OneOrMany<string>
* // string | readonly string[]
* // string | string[]
*
* @template T - Type to evaluate
*/
type OneOrMany<T> = T | readonly T[]
type OneOrMany<T> = T | T[]

export type { OneOrMany as default }
10 changes: 5 additions & 5 deletions src/types/overwrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type OneOrMany from './one-or-many'
* @template T - Target object
* @template U - Source object
*/
type Overwriter<T extends ObjectCurly, U> = IsEqual<T, U> extends true
type Overwriter<T extends Objectify<any>, U> = IsEqual<T, U> extends true
? T
: U extends ObjectCurly
? {
Expand All @@ -28,11 +28,11 @@ type Overwriter<T extends ObjectCurly, U> = IsEqual<T, U> extends true
} & {
[K in keyof U as HasKey<T, K> extends true ? K : never]: K
})]: HasKey<U, K> extends true
? U[K & keyof U]
? U[K]
: HasKey<T, K> extends true
? T[K & keyof T]
? T[K]
: never
} extends infer X extends ObjectCurly
} extends infer X extends Objectify<any>
? X
: never
: T
Expand All @@ -52,7 +52,7 @@ type Overwriter<T extends ObjectCurly, U> = IsEqual<T, U> extends true
*/
type Overwrite<
T extends ObjectCurly,
U extends OneOrMany<ObjectCurly> = EmptyObject
U extends Readonly<OneOrMany<ObjectCurly>> = EmptyObject
> = IsAnyOrNever<U> extends true
? Overwriter<T, Objectify<U>>
: T extends unknown
Expand Down

0 comments on commit 06ba1a6

Please sign in to comment.