arktype icon indicating copy to clipboard operation
arktype copied to clipboard

Morph keys

Open ssalbdivad opened this issue 1 year ago • 4 comments

Given a definition like:

const $ = scope({
  a: ["'a'", "=>", () => "b"],
  o: {
    "[a]": "string",
  },
});

We could parse o as an object of with morph type (In: {a: string}) => Out<{b: string}>.

Currently, this fails semantic validation as the morph types are not extracted.

ssalbdivad avatar Jul 05 '23 19:07 ssalbdivad

Hi, in addition to it. Is this possible? Not sure if it's related. image

viniciusflv avatar Sep 10 '24 20:09 viniciusflv

@viniciusflv This isn't really related to moprh keys. Luckily, it should actually be doable already!

The only runtime issue with the definition is that for an index signature, the brackets need to be in the key itself:

const attributes = type("<T extends string>", {"[T]": "string"})(camelCaseRegex)

If you're just using the generic to pass the regex, you can actually inline it if you prefer:

const t = type({"[/regexHere/]": "string"})

Note also that if you don't want to allow any keys that don't match that pattern, you'll want to add a "+": "reject" or "+": "delete" key to strip them.

ssalbdivad avatar Sep 10 '24 20:09 ssalbdivad

It works!!! Thanks 😊 Just curious. Why is this rejection not a default behavior?

viniciusflv avatar Sep 10 '24 20:09 viniciusflv

  1. It's how TypeScript works, which is good because...

  2. Checking for extraneous keys is much slower

  3. Usually they can be safely ignored

ssalbdivad avatar Sep 10 '24 20:09 ssalbdivad