ForbesLindesay / funtypes

Runtime validation for static types
MIT License
29 stars 4 forks source link

Feat: add keyOf type #55

Closed hinkik closed 2 years ago

hinkik commented 2 years ago

Adds a KeyOf type that can be used like this

myObject = {
  foo: "bar",
  4: "foobar",
}

const keyOfObjectSchema = ft.KeyOf("nameOfMyObjectKeys", myObject)

Potential issue: Object.Keys(...) returns string[] and thus other types than string keys become degenerated which means that

keyOfObjectSchema.parse("4") // "4"

even though "4" is not a member of keyof typeof myObject. Furthermore, other types that are not number will always fail the parsing.

Since all js object fields becomes strings, perhaps one needs to enforce the keys to be strings in the first place?

rollingversions[bot] commented 2 years ago

Change Log for funtypes (4.1.0 → 4.2.0)

New Features

Edit changelog