Skip to content

Commit

Permalink
Support for tuple type (#601)
Browse files Browse the repository at this point in the history
* fix tuple inference in the _Primitive type #600

* Processing for tuples and not #600

* For each element of the tuple, _Primivite not applied #600

* Define a Spread type to reduce type depth #600

* Just list each element #600

- There was a much simpler way.

* fix : _Primitive applied #600
  • Loading branch information
kakasoo authored Apr 24, 2023
1 parent b69ffda commit cb77f73
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Primitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export type Primitive<T> = _Equal<T, _Primitive<T>> extends true

type _Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;

type IsTuple<T extends readonly any[] | { length: number }> = [T] extends [
never,
]
? false
: T extends readonly any[]
? number extends T["length"]
? false
: true
: false;

type _Primitive<Instance> = _ValueOf<Instance> extends object
? Instance extends object
? Instance extends _Native
Expand All @@ -48,8 +58,14 @@ type _Primitive<Instance> = _ValueOf<Instance> extends object
: never // cannot be
: _ValueOf<Instance>;

type Spread<T extends any[]> = T extends [infer F, ...infer Rest]
? [_Primitive<F>, ...Spread<Rest>]
: [];

type _PrimitiveObject<Instance extends object> = Instance extends Array<infer T>
? _Primitive<T>[]
? IsTuple<Instance> extends true
? Spread<Instance>
: _Primitive<T>[]
: {
[P in keyof Instance]: Instance[P] extends Function
? never
Expand Down
22 changes: 22 additions & 0 deletions test/issues/600.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import typia from "typia";

console.log(
typia.random<readonly [1, 2, 3, 4, 5, 6]>(),
typia.random<[1, 2, 3, 4, 5, 6]>(),
typia.random<number[]>(),
typia.random<number[][]>(),
typia.random<boolean[][]>(),
typia.random<[number, boolean]>(),
typia.random<{ name: string }[]>(),
typia.random<[false, true, true, 2, 3, 4, "five", "six", "seven"]>(),
typia.random<[Boolean]>(),
typia.random<[Boolean[]]>(),

typia.createRandom<readonly [1, 2, 3, 4, 5, 6]>(),
typia.createRandom<[1, 2, 3, 4, 5, 6]>(),
typia.createRandom<number[]>(),
typia.createRandom<number[][]>(),
typia.createRandom<boolean[][]>(),
typia.createRandom<[number, boolean]>(),
typia.createRandom<{ name: string }[]>(),
);

0 comments on commit cb77f73

Please sign in to comment.