template literal/backtick string types e.g. type T = `type-${string}`
I can't see how to do in Ark:
I specifically want compile-time checking.
Tried to drop it in "1:1" style, but this doesn't work as-is:
nor did it work trying to use 'type' as a tagged template:
I did look in the docs but I don't believe it's there yet, and if it's in the tests I didn't spot it
Do I have to set it up as a custom string constraint so it'd be like
const UserId = type('string-userId')
?
IIUC this doesn't get the compile-time checking:
// Subtypes like 'email' are inferred like 'string' but provide additional validation at runtime. email: "string.email",
10 Replies
Ig this is what you're referring to: https://github.com/arktypeio/arktype/issues/491
GitHub
Template literal · Issue #491 · arktypeio/arktype
Would allow the equivalent TS syntax: // inferred as
a${string}z
[] const t = type("a${string}z
[]") Internally should probably compile to a regex.You'd need to use a morph/narrow for now (probably a narrow)
or whatever
Although the inferred type would just be
string
Yeah even if it didn't give me the exact template type I'd still be happy so long as it's a tagged/branded/opaque type that forced the string through a type predicate before use
Opaque / Branded Types in Typescript
Leveling up your compile-time type-checking in Typescript!
hm, this won't work consistently either:
I wonder if ark should allow you to supply/override the type you want it to infer as
e.g. something like
whoo
perfect
thank you
Ohhh yeahhhhh RegEx 🤩
this doesn't work (?)
playground: https://arktype.io/playground?code=import%2520%257B%2520type%2520%257D%2520from%2520%2522arktype%2522%250A%250Aconst%2520Thing%2520%253D%2520type%28%252F%255Euser-%252F%29.as%253C%2560user-%2524%257Bstring%257D%2560%253E%28%29%250A%250Aconst%2520out%2520%253D%2520Thing%28%2522user-dada%2522%29%250A
Seems like an issue with the playground parser. It should work in a real environment
Figured out the playground issue, will be fixed in the next release!