#322·io-ts

Is there a way to write a codec that fails on additional properties?

Author: MousakaCreated Apr 30, 2019Updated Sep 30, 2025

Do you want to request a feature or report a bug? Question: Is there a way to write a codec that fails on additional properties?

typescript
const FruitOrAnimal = t.union([
  t.strict({ fruit: t.string }),
  t.strict({ animal: t.string })
]);

console.log(FruitOrAnimal.decode({ fruit: "banana", animal: "cat" }).isRight());
//prints  'true' but would like it to be 'false'.

Would like a decoder that fails this input type and interpret it as an "impossible state". An input with either fruit or animal is fine but never both.

What is the current behavior? https://codesandbox.io/embed/8483r1pwq9

Which versions of io-ts, and which browser and OS are affected by this issue? Did this work in previous versions of io-ts? 1.8.5