From bfd1e9c99c16169600f42a988b1ac4fb0452f749 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sun, 29 Oct 2023 16:44:45 +0100 Subject: [PATCH] Remove the second type variable of `Decoder` (#39) --- CHANGELOG.md | 18 ++++++++++++++++++ README.md | 10 ---------- examples/type-annotations.test.ts | 4 ++-- index.ts | 2 +- tests/decoders.test.ts | 4 ++-- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb1dad7..f2594e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,23 @@ Note: I’m currently working on several breaking changes to tiny-decoders, but I’m trying out releasing them piece by piece. The idea is that you can either upgrade version by version only having to deal with one or a few breaking changes at a time, or wait and do a bunch of them at the same time. +### Version 18.0.0 (unreleased) + +This release removes the second type variable from `Decoder`. + +Before: + +```ts +type Decoder = (value: U) => DecoderResult; +``` + +After: + +```ts +type Decoder = (value: unknown) => DecoderResult; +``` + +This change unlocks further changes that will come in future releases. + ### Version 17.0.1 (2023-10-29) Fixed: `fieldsAuto` now reports the correct field name when there’s an error in a renamed field. diff --git a/README.md b/README.md index c2b2367..86fd3e5 100644 --- a/README.md +++ b/README.md @@ -106,16 +106,6 @@ That’s it! tiny-decoders ships with a bunch of decoders, and a few functions to combine decoders. This way you can describe the shape of any data! -### Advanced variant - -```ts -type Decoder = (value: U) => DecoderResult; -``` - -The above is the _full_ definition of a decoder. The input value can be some other type (`U`) than `unknown` if you want. - -Most of the time you don’t need to think about this, though! - ## Decoders Here’s a summary of all decoders (with slightly simplified type annotations): diff --git a/examples/type-annotations.test.ts b/examples/type-annotations.test.ts index e57d00f..6e1b62c 100644 --- a/examples/type-annotations.test.ts +++ b/examples/type-annotations.test.ts @@ -41,8 +41,8 @@ test("type annotations", () => { // The way to make the above type error more clear is to provide an explicit type // annotation, so that TypeScript knows what you’re trying to do. - // @ts-expect-error Type 'Decoder<{ name: string; aye: number; }, unknown>' is not assignable to type 'Decoder'. - // Property 'age' is missing in type '{ name: string; aye: number; }' but required in type 'Person'.ts(2322) + // @ts-expect-error Type 'Decoder<{ name: string; aye: number; }>' is not assignable to type 'Decoder'. + // Property 'age' is missing in type '{ name: string; aye: number; }' but required in type 'Person'. const personDecoder2: Decoder = fieldsAuto({ name: string, aye: number, diff --git a/index.ts b/index.ts index 1823823..7c43bf2 100644 --- a/index.ts +++ b/index.ts @@ -2,7 +2,7 @@ // No `any` “leaks” when _using_ the library, though. /* eslint-disable @typescript-eslint/no-explicit-any */ -export type Decoder = (value: U) => DecoderResult; +export type Decoder = (value: unknown) => DecoderResult; export type DecoderResult = | { diff --git a/tests/decoders.test.ts b/tests/decoders.test.ts index 78209fc..6c16924 100644 --- a/tests/decoders.test.ts +++ b/tests/decoders.test.ts @@ -1022,9 +1022,9 @@ describe("tag", () => { }); describe("tuple", () => { - // @ts-expect-error Argument of type '{}' is not assignable to parameter of type 'readonly Decoder[]'. + // @ts-expect-error Argument of type '{}' is not assignable to parameter of type 'Decoder[]'. tuple({}); - // @ts-expect-error Argument of type '(value: unknown) => number' is not assignable to parameter of type 'readonly Decoder[]'. + // @ts-expect-error Argument of type '(value: unknown) => DecoderResult' is not assignable to parameter of type 'Decoder[]'. tuple(number); test("0 items", () => {