Skip to content

Commit

Permalink
Make $array and $tuple readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
kshramt committed Dec 30, 2023
1 parent 41db141 commit 504d273
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
13 changes: 10 additions & 3 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,14 @@ type NExpect<T extends false> = T;
}
{
const validator = T.$tuple([T.$number(), T.$string()]);
type _ = Expect<Check<T.$infer<typeof validator>, [number, string]>>;
type _ = Expect<
Check<T.$infer<typeof validator>, readonly [number, string]>
>;
}
{
const validator = T.$tuple([T.$number(), T.$string()], T.$boolean());
type _ = Expect<
Check<T.$infer<typeof validator>, [number, string, ...boolean[]]>
Check<T.$infer<typeof validator>, readonly [number, string, ...boolean[]]>
>;
}
{
Expand All @@ -377,7 +379,12 @@ type NExpect<T extends false> = T;
type _ = Expect<
Check<
T.$infer<typeof validator>,
{ a: null; b: undefined; c: boolean; d: null | [string[]] }
{
a: null;
b: undefined;
c: boolean;
d: null | readonly [readonly string[]];
}
>
>;
}
Expand Down
20 changes: 13 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,25 @@ export const $typeGuard = <T>(
};
};

export function $tuple<T extends unknown[]>(
validators: [...{ [K in keyof T]: TValidator<T[K]> }],
): (value: unknown, path?: TRef<TPath>) => value is T;
export function $tuple<TTuple extends unknown[]>(
validators: [...{ [K in keyof TTuple]: TValidator<TTuple[K]> }],
): (value: unknown, path?: TRef<TPath>) => value is readonly [...TTuple];
export function $tuple<TTuple extends unknown[], TRest>(
validators: [...{ [K in keyof TTuple]: TValidator<TTuple[K]> }],
rest: TValidator<TRest>,
): (value: unknown, path?: TRef<TPath>) => value is [...TTuple, ...TRest[]];
): (
value: unknown,
path?: TRef<TPath>,
) => value is readonly [...TTuple, ...TRest[]];
export function $tuple<TTuple extends unknown[], TRest>(
validators: [...{ [K in keyof TTuple]: TValidator<TTuple[K]> }],
rest?: TValidator<TRest>,
) {
if (rest === undefined) {
return (value: unknown, path?: TRef<TPath>): value is TTuple => {
return (
value: unknown,
path?: TRef<TPath>,
): value is readonly [...TTuple] => {
if (!Array.isArray(value)) {
if (path) {
path.value = { not_array: value };
Expand Down Expand Up @@ -130,7 +136,7 @@ export function $tuple<TTuple extends unknown[], TRest>(
return (
value: unknown,
path?: TRef<TPath>,
): value is [...TTuple, ...TRest[]] => {
): value is readonly [...TTuple, ...TRest[]] => {
if (!Array.isArray(value)) {
if (path) {
path.value = { not_array: value };
Expand Down Expand Up @@ -192,7 +198,7 @@ export const $literal = <T extends TNarrowable>(val: T) => {
};

export const $array = <T>(validator: TValidator<T>) => {
return (value: unknown, path?: TRef<TPath>): value is T[] => {
return (value: unknown, path?: TRef<TPath>): value is readonly T[] => {
if (!Array.isArray(value)) {
if (path) {
path.value = { not_array: value };
Expand Down

0 comments on commit 504d273

Please sign in to comment.