ForbesLindesay / funtypes

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

Exact / Sealed / Closed objects #12

Open ForbesLindesay opened 3 years ago

ForbesLindesay commented 3 years ago

There is currently no way to disallow additional properties on records. The closest you could get would be to Intersect the Record with a Dictionary that specifies the allowed fields.

Proposed API:


interface Record<T> {
  // ...existing props and methods...
  asExact(): this;
}
ForbesLindesay commented 3 years ago

https://github.com/pelotom/runtypes/pull/162 looks like a pretty good start. I'm inclined not to attempt to make it a generic parameter, since it doesn't really affect the TypeScript type of the parsed value.

(Would also resolve https://github.com/pelotom/runtypes/issues/169 and https://github.com/pelotom/runtypes/issues/41)

ForbesLindesay commented 2 years ago

Added in #56