A
arktype•5w ago
terinjokes

morphing to typescript type

I tried to do a narrow + pipe.try on type("string") to try to parse out a Temporal.ZonedDateTime type to sort of replicate what type("string.date.iso.parse") does, and it mostly works. But I've noticed if I infer the TS type I can't actually assign a Temporal.ZoneDateTime to the field, as tsc complains that the valueOf signatures are different. Is this supposed to work, or should I be venturing down the path of scopes and rootSchema instead?
const zonedDateTimeISO = type("string")
.narrow((date, ctx) => {
if (zoneDateTimeRegex.test(date)) { return true; }

return ctx.mustBe("an ISO-8601 datetime string");
})
.pipe.try((s: string) => Temporal.ZonedDateTime.from(s));

const struct = type({timestamp: zonedDateTimeISO});
type Struct = typeof struct.infer;

const example : Struct = {
timestamp: Temporal.Now.zonedDateTimeIso(), // tsc fails here
}
const zonedDateTimeISO = type("string")
.narrow((date, ctx) => {
if (zoneDateTimeRegex.test(date)) { return true; }

return ctx.mustBe("an ISO-8601 datetime string");
})
.pipe.try((s: string) => Temporal.ZonedDateTime.from(s));

const struct = type({timestamp: zonedDateTimeISO});
type Struct = typeof struct.infer;

const example : Struct = {
timestamp: Temporal.Now.zonedDateTimeIso(), // tsc fails here
}
19 Replies
terinjokes
terinjokesOP•4w ago
Not being able to assign the datetime to the field has resulted in a pretty large kludge today. Is there an option I'm missing?
TizzySaurus
TizzySaurus•4w ago
This is the sort of thing that's very difficult to help with without running it ourselves, and unfortunately there's no playground yet for ArkType v2.0 (tbh this is imo the biggest regression from v1.0). I'm on my PC now so will give this a look.... Can you send zoneDateTimeRegex?
TizzySaurus
TizzySaurus•4w ago
And just to confirm, are you using https://www.npmjs.com/package/@js-temporal/polyfill for temporal?
npm
@js-temporal/polyfill
Polyfill for Tc39 Stage 3 proposal Temporal (https://github.com/tc39/proposal-temporal). Latest version: 0.4.4, last published: a year ago. Start using @js-temporal/polyfill in your project by running npm i @js-temporal/polyfill. There are 113 other projects in the npm registry using @js-temporal/polyfill.
terinjokes
terinjokesOP•4w ago
I want to apologize for it first.
const zoneDateTimeRegex =
/^[+\u22120-]?(?:\d{6}|\d{4})-?\d{2}-?\d{2}[T ]\d{2}:?\d{2}:?\d{2}(?:[.,]\d{1,9})?(?:Z|[+\u2212-]?\d{2}:?\d{2}(?::?\d{2})?(?:[.,]\d{1,9})?)?(?:\[(?:!?)(?:[^\]]*)\]){1,9}$/i;
const zoneDateTimeRegex =
/^[+\u22120-]?(?:\d{6}|\d{4})-?\d{2}-?\d{2}[T ]\d{2}:?\d{2}:?\d{2}(?:[.,]\d{1,9})?(?:Z|[+\u2212-]?\d{2}:?\d{2}(?::?\d{2})?(?:[.,]\d{1,9})?)?(?:\[(?:!?)(?:[^\]]*)\]){1,9}$/i;
yes
TizzySaurus
TizzySaurus•4w ago
Fwiw the error I'm getting is this one:
No description
TizzySaurus
TizzySaurus•4w ago
It seems to want ISO rather than Iso at the end
terinjokes
terinjokesOP•4w ago
oh, sorry, I must have made that mistake while typing it up here
TizzySaurus
TizzySaurus•4w ago
Ignoring that, I'm getting this. Is this what you meant by "the valueOf signatures are different"?
No description
terinjokes
terinjokesOP•4w ago
yes
TizzySaurus
TizzySaurus•4w ago
Ah yeah, I see it now:
No description
TizzySaurus
TizzySaurus•4w ago
Temporal's ZonedDataTime has
valueOf(): never;
valueOf(): never;
so that part is correct. I'm not entirely sure where the never is coming from.
TizzySaurus
TizzySaurus•4w ago
This is what typeof struct.infer is giving:
No description
TizzySaurus
TizzySaurus•4w ago
This looks like it might just be a bug in ArkType. It's for some reason stripping the function part
TizzySaurus
TizzySaurus•4w ago
This is ultimately the issue. struct.infer eventually bubbles down to valueOf: _distil<() => never, {endpoint: "out"}> and _distil<() => never, {endpoint: "out"}> is never.
No description
TizzySaurus
TizzySaurus•4w ago
I assume this is because () => never extends InferredMorph, which I don't think it should. @ssalbdivad Are you able to confirm that this is the case (i.e. this is unintentional)? Context: the inferred type of a morph that returns an object with valueOf: () => never becomes valueOf: never (instead of keeping valueOf as a function, as it does for when e.g. valueOf: () => number) Tl;Dr; I'm fairly certain this is a bug in ArkType (tbc by David), so thanks for reporting this 🙂
terinjokes
terinjokesOP•4w ago
i can file a proper GitHub issue if you want. I had assumed I was using it wrong.
TizzySaurus
TizzySaurus•4w ago
Err, you can do. Might be worth waiting for David to confirm it is indeed a bug.
ssalbdivad
ssalbdivad•4w ago
This is a known design limitation but unfortunately there is not a performant way to check for functions returning never specifically without incurring a lot of overhead for a fairly niche case. The same is true for functions returning any However, the ideal solution for problems like these is just for ArkType to treat it as a known builtin object and not recurse into it. I will add the temporal objects to the list of builtins once they're generally available, but for now you can add additional builtins to ignore during inference like this:
declare global {
export interface ArkEnv {
prototypes(): Temporal.ZonedDateTime | OtherKnownClasses
}
}
declare global {
export interface ArkEnv {
prototypes(): Temporal.ZonedDateTime | OtherKnownClasses
}
}
terinjokes
terinjokesOP•4w ago
I added that declaration and it worked.
Want results from more Discord servers?
Add your server