SynthLuvr
SynthLuvr
Aarktype
Created by SynthLuvr on 9/6/2024 in #questions
Type instantiation is excessively deep and possibly infinite
const A = type("bigint").narrow(() => true).pipe((v) => v.toString());
type A = typeof A.in.infer;
const A = type("bigint").narrow(() => true).pipe((v) => v.toString());
type A = typeof A.in.infer;
Produces error:
Type instantiation is excessively deep and possibly infinite.
Type instantiation is excessively deep and possibly infinite.
52 replies
Aarktype
Created by SynthLuvr on 9/4/2024 in #questions
Record with number as key?
const A = type("Record<number, string>");
const A = type("Record<number, string>");
This throws error:
Argument of type '"Record<number, string>"' is not assignable to parameter of type '"K must be assignable to string | symbol (was number) "'
Argument of type '"Record<number, string>"' is not assignable to parameter of type '"K must be assignable to string | symbol (was number) "'
But it should be valid:
type A = Record<number, string>;
type A = Record<number, string>;
6 replies
Aarktype
Created by SynthLuvr on 9/2/2024 in #questions
Compiling to JavaScript results in `any` type
I have a TypeScript library that compiles down into JavaScript, and one of the d.ts files looks like this:
declare const Address: import("arktype/out/subtypes/string").StringType<import("arktype/out/keywords/ast").string.narrowed, {}>;
type Address = typeof Address.in.infer;
declare const Address: import("arktype/out/subtypes/string").StringType<import("arktype/out/keywords/ast").string.narrowed, {}>;
type Address = typeof Address.in.infer;
In this example, Address is of type any, whereas when I'm in TypeScript it's of type string
3 replies
Aarktype
Created by SynthLuvr on 9/2/2024 in #questions
Was `any` removed?
I used to be able to do: type("any");. Now it's throwing error Argument of type '"any"' is not assignable to parameter of type '"'any' is unresolvable "'
15 replies
Aarktype
Created by SynthLuvr on 9/2/2024 in #questions
Type instantiation is excessively deep and possibly infinite
const m = <T, U>(type: Type<T, U>) => {
return type.describe("test");
}
const m = <T, U>(type: Type<T, U>) => {
return type.describe("test");
}
This used to work for me in in 2.0.0-dev.29. But upgrading to 2.0.0-rc.5 I get:
Type instantiation is excessively deep and possibly infinite.ts(2589)
(method) Type<out t = unknown, $ = {}>.describe(description: string): Type<T, U> | Type<T, U>
Type instantiation is excessively deep and possibly infinite.ts(2589)
(method) Type<out t = unknown, $ = {}>.describe(description: string): Type<T, U> | Type<T, U>
18 replies
Aarktype
Created by SynthLuvr on 8/6/2024 in #questions
ArkType mutates original input?
I was always under the assumption that ArkType leaves the original input intact. However:
import { type } from "arktype";

const original = { value: "100" };
const Parser = type({ value: type("string").pipe(BigInt) });
Parser.assert(original);
console.log(original);
import { type } from "arktype";

const original = { value: "100" };
const Parser = type({ value: type("string").pipe(BigInt) });
Parser.assert(original);
console.log(original);
Outputs:
{ value: 100n }
{ value: 100n }
My assumption was that value would remain as string, and the result returned by Parser would be this value. But based on this, it seems like I was under the wrong assumption.
74 replies
Aarktype
Created by SynthLuvr on 7/13/2024 in #questions
ParseError: Root of kind union should be one of alias,intersection,unit,domain,proto
import { type } from "arktype";

const Amount = type("string|number")
.pipe((s, ctx) => {
try {
return BigInt(s);
} catch {
return ctx.error("a non-decimal number");
}
})
.narrow((amount, ctx) => amount > 0n || ctx.mustBe("greater than zero"));

console.log(Amount.assert("1000"));
import { type } from "arktype";

const Amount = type("string|number")
.pipe((s, ctx) => {
try {
return BigInt(s);
} catch {
return ctx.error("a non-decimal number");
}
})
.narrow((amount, ctx) => amount > 0n || ctx.mustBe("greater than zero"));

console.log(Amount.assert("1000"));
Produces error:
ParseError: Root of kind union should be one of alias,intersection,unit,domain,proto
ParseError: Root of kind union should be one of alias,intersection,unit,domain,proto
8 replies
Aarktype
Created by SynthLuvr on 6/12/2024 in #questions
Can you create types with dynamic literals?
Say I have this function:
const createType = <T extends string>(code: T) => {
const literal: string = code;
return type({
code: `'${literal}'`
});
};
const createType = <T extends string>(code: T) => {
const literal: string = code;
return type({
code: `'${literal}'`
});
};
With this I'm trying to create types with literals but I don't know those literals until runtime. If I run:
const TypeONE = createType("ONE");
const TypeONE = createType("ONE");
It creates type:
const TypeONE: Type<{
code: string;
}, {}>
const TypeONE: Type<{
code: string;
}, {}>
But instead what I'm actually wanting is:
const TypeONE = type({
code: "'ONE'"
});
const TypeONE = type({
code: "'ONE'"
});
Which produces the correct type:
const TypeONE: Type<{
code: "ONE";
}, {}>
const TypeONE: Type<{
code: "ONE";
}, {}>
43 replies
Aarktype
Created by SynthLuvr on 6/11/2024 in #questions
Cannot assign to Type
import { scope, Type } from "arktype";

const ValidationErrorResponse = scope({
ErrorDescription: {
message: "string",
code: "'VALIDATION'",
},
Response: {
code: "400",
contents: {
errors: "ErrorDescription[]>0",
},
},
}).export().Response;

const mytype: Type = ValidationErrorResponse;
import { scope, Type } from "arktype";

const ValidationErrorResponse = scope({
ErrorDescription: {
message: "string",
code: "'VALIDATION'",
},
Response: {
code: "400",
contents: {
errors: "ErrorDescription[]>0",
},
},
}).export().Response;

const mytype: Type = ValidationErrorResponse;
This works fine in version 2.0.0-dev.19. However, updating to 2.0.0-dev.20 or above it throws this error:
Type 'Type<{ code: 400; contents: { errors: of<{ message: string; code: "VALIDATION"; }[], MoreThanLength<0>>; }; }, { Response: { code: 400; contents: { errors: of<{ message: string; code: "VALIDATION"; }[], MoreThanLength<...>>; }; }; ErrorDescription: { ...; }; }>' is not assignable to type 'Type<unknown, {}>'.
The types returned by '$.type(...)' are incompatible between these types.
Type 'Type<any, { Response: { code: 400; contents: { errors: of<{ message: string; code: "VALIDATION"; }[], MoreThanLength<0>>; }; }; ErrorDescription: { message: string; code: "VALIDATION"; }; }>' is not assignable to type 'Type<any, {}>'.
Type '{}' is not assignable to type '{ Response: { code: 400; contents: { errors: of<{ message: string; code: "VALIDATION"; }[], MoreThanLength<0>>; }; }; ErrorDescription: { message: string; code: "VALIDATION"; }; }'.
Type 'Type<{ code: 400; contents: { errors: of<{ message: string; code: "VALIDATION"; }[], MoreThanLength<0>>; }; }, { Response: { code: 400; contents: { errors: of<{ message: string; code: "VALIDATION"; }[], MoreThanLength<...>>; }; }; ErrorDescription: { ...; }; }>' is not assignable to type 'Type<unknown, {}>'.
The types returned by '$.type(...)' are incompatible between these types.
Type 'Type<any, { Response: { code: 400; contents: { errors: of<{ message: string; code: "VALIDATION"; }[], MoreThanLength<0>>; }; }; ErrorDescription: { message: string; code: "VALIDATION"; }; }>' is not assignable to type 'Type<any, {}>'.
Type '{}' is not assignable to type '{ Response: { code: 400; contents: { errors: of<{ message: string; code: "VALIDATION"; }[], MoreThanLength<0>>; }; }; ErrorDescription: { message: string; code: "VALIDATION"; }; }'.
3 replies
Aarktype
Created by SynthLuvr on 6/11/2024 in #questions
How to validate `Record<string, string>`?
My naïve attempt:
const Response = type({
"*": "string",
});
type Response = typeof Response.infer;

const createResponse = (): Response => {
const values: Record<string, string> = {
value1: "one",
value2: "two",
};
return values;
};
const Response = type({
"*": "string",
});
type Response = typeof Response.infer;

const createResponse = (): Response => {
const values: Record<string, string> = {
value1: "one",
value2: "two",
};
return values;
};
This results in error:
Property '"*"' is missing in type 'Record<string, string>' but required in type '{ "*": string; }'.
Property '"*"' is missing in type 'Record<string, string>' but required in type '{ "*": string; }'.
7 replies
Aarktype
Created by SynthLuvr on 6/11/2024 in #questions
Property 'name' is missing in type
const MyErrorResponse = scope({
Error: {
message: "string",
code: "string",
},
Response: {
code: "number==500",
contents: {
errors: "Error[]",
},
},
}).export().Response;
type MyErrorResponse = typeof MyErrorResponse.infer;

const createErrorResponse = (): MyErrorResponse => {
return {
code: 500,
contents: {
errors: [{ message: "example", code: "ERROR" }],
},
};
};
const MyErrorResponse = scope({
Error: {
message: "string",
code: "string",
},
Response: {
code: "number==500",
contents: {
errors: "Error[]",
},
},
}).export().Response;
type MyErrorResponse = typeof MyErrorResponse.infer;

const createErrorResponse = (): MyErrorResponse => {
return {
code: 500,
contents: {
errors: [{ message: "example", code: "ERROR" }],
},
};
};
This results in error:
Property 'name' is missing in type '{ message: string; code: string; }' but required in type '{ message: string; code: string; name: never; stack?: undefined; cause?: undefined; }'.
Property 'name' is missing in type '{ message: string; code: string; }' but required in type '{ message: string; code: string; name: never; stack?: undefined; cause?: undefined; }'.
This can be resolved by using type:
const MyErrorResponse = scope({
Error: type({
message: "string",
code: "string",
}),
Response: {
code: "number==500",
contents: {
errors: "Error[]",
},
},
}).export().Response;
const MyErrorResponse = scope({
Error: type({
message: "string",
code: "string",
}),
Response: {
code: "number==500",
contents: {
errors: "Error[]",
},
},
}).export().Response;
Is this the intended usage, or a bug? Because I was expecting that type would be implied within scope
15 replies
Aarktype
Created by SynthLuvr on 6/8/2024 in #questions
Type 'distillOut<T>' is not assignable to type 'T'.
import { type, Type } from "arktype";

const test = <T>(validator: Type<T>): T => {
const result = validator({});
if (result instanceof type.errors) throw new Error();
return result;
}
import { type, Type } from "arktype";

const test = <T>(validator: Type<T>): T => {
const result = validator({});
if (result instanceof type.errors) throw new Error();
return result;
}
This produces error:
Type 'distillOut<T>' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'distillOut<T>'.
Type 'distillOut<T>' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'distillOut<T>'.
If I cast, then it works:
import { type, Type } from "arktype";

const test = <T>(validator: Type<T>): T => {
const result = validator({});
if (result instanceof type.errors) throw new Error();
return result as T;
}
import { type, Type } from "arktype";

const test = <T>(validator: Type<T>): T => {
const result = validator({});
if (result instanceof type.errors) throw new Error();
return result as T;
}
Why is the casting necessary? Shouldn't it be type T after checking it's not type.errors?
25 replies
Aarktype
Created by SynthLuvr on 5/25/2024 in #questions
How to improve error messages?
address must be a string and less than length 200 and more than length 50 and valid according to an anonymous predicate and valid according to an anonymous predicate and valid according to an anonymous predicate (was missing)
This is an example of a validation error message returned. How could I go about making that a little more human friendly?
7 replies
Aarktype
Created by SynthLuvr on 5/24/2024 in #questions
Pipe required before narrow
const myFunc = (v: string) => true;
const MyString = type("50<string<200").narrow((s) => myFunc(s));
const myFunc = (v: string) => true;
const MyString = type("50<string<200").narrow((s) => myFunc(s));
Results in error:
error TS2345: Argument of type 'is<MoreThanLength<50> & LessThanLength<200>>' is not assignable to parameter of type 'string'.
error TS2345: Argument of type 'is<MoreThanLength<50> & LessThanLength<200>>' is not assignable to parameter of type 'string'.
However, adding pipe before calling narrow works:
const myFunc = (v: string) => true;
const MyString = type("50<string<200").pipe((s) => s).narrow((s) => myFunc(s));
const myFunc = (v: string) => true;
const MyString = type("50<string<200").pipe((s) => s).narrow((s) => myFunc(s));
Is this intentional?
5 replies
Aarktype
Created by SynthLuvr on 5/23/2024 in #questions
Error transforming object
I'm having some trouble doing transforming some data
9 replies
Aarktype
Created by SynthLuvr on 5/20/2024 in #questions
Problem with Out type
Type '(In: string) => Out<bigint>' is not assignable to type 'bigint'
Type '(In: string) => Out<bigint>' is not assignable to type 'bigint'
29 replies