Simple question re a function that returns a type:
Trying to create simple "factory" functions that make a type based on some other type. This works fine:
But if it's
"string.integer.parse"
it fails to compile.
Any hint as to why? Do I have a problem with my makeBar()
prototype?16 Replies
Wdym by "fails to compile"?
Do you get a runtime error? If so, what's the error?
Compilation error:
Does work?
that doesn't even compile without the
.parse
:
Is this a common pattern to have a function that takes a type and returns a new type?I'd say so, yeah. There's even an example of it on the docs: https://arktype.io/docs/generics#external
interesting
ok not sure how I missed it.
Does this work if you explicitly type the return? I.e.
Type<{weird: boolean, in: T1, out: T2}>
worth noting that just going one additional level also break compilation:
Let me try
I don't really know how to get that type
As in
Just curious if that makes it work
oh I see
it works in the
string.integer
setup, but adding .parse
still breaks compilationYeah, not sure then I'm afraid. Maybe @ssalbdivad can give some insight
Hmm I'm really not sure why this comparison is even happening- seems like a possible TS bug? Maybe @Andarist would have some insight from that perspective.
A workaround is to accept
type.cast
instead, which is basically "anything arktype recognizes as inferred like X" without checking all the type methods etc. which simplifies the inference and seems to work here:
looks buggy to me but who knows
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
usually i'd say the return type inference is at fault here but explicit type arguments turn off inference altogether so 🤷♂️
as always - I could investigate a small repro case of this 😛
fwiw, it's ur weird
: r extends infer _ ? _ : never
return type that breaks this
and ofc a regular NoInfer
fixes this issue:
nice! does this address the original issue? I just updated the playground link above to include the other line that fails to compile.