Skip to content

v4.2.0

Choose a tag to compare

@joanllenas joanllenas released this 16 Sep 21:18
· 2 commits to master since this release
cf26e3a

What's Changed

  • Add a new ts.data.json/mini entry point. It is the same library without the Decoder class. A decoder is a plain function (json) => Result<T>, and the methods become standalone imports (decode, parse, decodePromise, map, flatMap).
  • oneOf now infers the union of its alternatives, so oneOf([string(), number()]) gives Decoder<string | number> without a type argument. The explicit form oneOf<string | number>([...]) still works.
  • Move the decoding logic into shared internal functions used by both entry points. Both entry points validate the same way and report the same issues.
import * as J from 'ts.data.json/mini'

const user = J.object({ id: J.number(), name: J.string() })
type User = J.FromDecoder<typeof user>

J.decode(user, json) // Result<User>, never throws
J.parse(user, json) // User, throws on failure
J.map(J.string(), iso => new Date(iso))

mini saves about 1 kB minified. That matters most for apps that import only a few decoders:

What you import main entry mini
string() alone 1457 B 452 B
object + string + number + array + optional 2562 B 1451 B

Things to know:

  • Decoders from the two entry points are different types and cannot be mixed. Pick one per project.
  • null and undefined are exported under those names. A named import needs an alias: import { null as jsonNull } from 'ts.data.json/mini'.
  • In mini, Standard Schema support is opt-in through toStandardSchema(decoder), so apps that do not use it do not bundle it.

Docs

New mini entry point section in the README, and a mini API reference. The API docs sidebar now lists the main and mini entry points separately.

Full Changelog: v4.1.0...v4.2.0