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?
19 Replies
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?
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
?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.I want to apologize for it first.
yes
Fwiw the error I'm getting is this one:
It seems to want
ISO
rather than Iso
at the endoh, sorry, I must have made that mistake while typing it up here
Ignoring that, I'm getting this. Is this what you meant by "the
valueOf
signatures are different"?yes
Ah yeah, I see it now:
Temporal's
ZonedDataTime
has so that part is correct. I'm not entirely sure where the never
is coming from.This is what
typeof struct.infer
is giving:This looks like it might just be a bug in ArkType. It's for some reason stripping the function part
This is ultimately the issue.
struct.infer
eventually bubbles down to valueOf: _distil<() => never, {endpoint: "out"}>
and _distil<() => never, {endpoint: "out"}>
is never
.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 🙂i can file a proper GitHub issue if you want. I had assumed I was using it wrong.
Err, you can do. Might be worth waiting for David to confirm it is indeed a bug.
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:
I added that declaration and it worked.