terinjokes
terinjokes
Aarktype
Created by terinjokes on 11/4/2024 in #questions
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
}
24 replies