Defining Records with narrowed keys
I couldn't find a way in the docs on object types (https://arktype.io/docs/objects) to define a record with narrowed keys. For example keys with no special symbols in them:
BAD
GOOD
Seems like there should be a way, but I haven't been able to locate it.
7 Replies
You can use index signatures for anything regex-compatible
nice! Is error messaging configurable on that in the case of a failure?
Iirc you can use
.configure
for that
E.g. Ok, I can give that a shot. Cheers 🙏
Also, not really clear how I configure just the index type, and not the value type.
@TizzySaurus I can't seem to get this to work in the playground https://arktype.io/playground?code=import%2520%257B%2520type%2520%257D%2520from%2520%2522arktype%2522%250A%250Aconst%2520Thing%2520%253D%2520type%28%257B%250A%2509%27%252F%255Ba-z%255D%252B%252F%27%253A%2520%2522string%2522%252C%250A%257D%29%250A%250Aconst%2520out%2520%253D%2520Thing%28%257B%2522username%2522%253A%2520%2522bob%2522%257D%29%250A
If you look at the docs you can override specific error codes, I.e. figure out the error code for violating the key and overwrite its message
I guess I've forgotten how to do index signatures correctly. You can scan through the docs / unit tests to figure that out