decoders

Tips

Practical tips for using decoders effectively.

The difference between object, exact, and inexact

The three decoders in the "object" family — object(), exact() and inexact() — are very similar and only differ in how they treat extra properties on input values.

For example, for a definition like:

import { exact, inexact, number, object, string } from "decoders";

const thing = { a: string, b: number };

And a runtime input of:

{
  a: "hi",
  b: 42,
  c: "extra",  // Note "c" is not a known field
}
Extra propertiesOutput valueInferred type
object(thing)discarded{a: "hi", b: 42}{a: string, b: number}
exact(thing)not allowedn/a (rejected){a: string, b: number}
inexact(thing)retained{a: "hi", b: 42, c: "extra"}{a: string, b: number, [string]: unknown}

On this page