A
arktypeβ€’2w ago
ciscoheat

Empty string or number

I have this type that I want to reuse:
const number = type("string")
.pipe((s) => s || "0")
.to("string.numeric.parse");
const number = type("string")
.pipe((s) => s || "0")
.to("string.numeric.parse");
I want to reuse it so if the string is empty, leave it as is, otherwise parse it as the number type. But when I do this, it complains:
type("''").or(number); // An unordered union of a type including a morph and a type with overlapping input is indeterminate
type("''").or(number); // An unordered union of a type including a morph and a type with overlapping input is indeterminate
This works though, so it's no big deal but just curious if it's possible to combine these above?
type("''").or("string.numeric.parse");
type("''").or("string.numeric.parse");
10 Replies
ciscoheat
ciscoheatOPβ€’2w ago
I also have a currency type that I may want to apply the same logic to:
type("string | number").pipe((s) => currency(s))
type("string | number").pipe((s) => currency(s))
But same problem there. It could be solved in the pipe function, but it would be nice to just be able to add an empty string type to another type, to avoid rewriting functions.
πŸŒΊπŸ‡«πŸ‡· Shigu :3 🌺
why not apply the number type through another pipe? ie:
type("string").pipe((s) => s.length !== 0 ? number(s) : "");
type("string").pipe((s) => s.length !== 0 ? number(s) : "");
if you want you could put the code adding the pipe into a function and use that instead
ciscoheat
ciscoheatOPβ€’2w ago
That works, but I'd like to be able, if possible, to use a type directly so it can be easily chained to existing types
ssalbdivad
ssalbdivadβ€’2w ago
The key is with overlapping input. You just have to make sure if one branch of the union contains a transform, we can tell they have no overlap:
// make the string non-empty to remove the overlap
const number = type("string > 0")
.pipe(s => s || "0")
.to("string.numeric.parse")

// now works fine
number.or("''")
// make the string non-empty to remove the overlap
const number = type("string > 0")
.pipe(s => s || "0")
.to("string.numeric.parse")

// now works fine
number.or("''")
In this case I don't think the pipe would ever apply, but hopefully that concept is helpful Or rather, the pipe would never do anything since s will always be truthy
ciscoheat
ciscoheatOPβ€’2w ago
Makes sense, thank you! Just started up a Deno project and used Arktype for CSV -> object validation, so incredibly convenient. Thank you again ☺️
ssalbdivad
ssalbdivadβ€’2w ago
Glad it's been going well! Still have so much I want to build + document
ciscoheat
ciscoheatOPβ€’2w ago
Yeah, I'd love to use the JSON Schema feature to improve the adapter for Superforms
ssalbdivad
ssalbdivadβ€’2w ago
Close on that one and I'm really happy with the API
No description
ssalbdivad
ssalbdivadβ€’2w ago
Basically you can handle each incompatibility individually or apply a fallback to all of them at once
ciscoheat
ciscoheatOPβ€’2w ago
Ohh, great Looking forward to that one!

Did you find this page helpful?