Skip to content

Commit

Permalink
Remove the second type variable of Decoder (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
lydell authored Oct 29, 2023
1 parent d752fd4 commit bfd1e9c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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<T, U = unknown> = (value: U) => DecoderResult<T>;
```

After:

```ts
type Decoder<T> = (value: unknown) => DecoderResult<T>;
```

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.
Expand Down
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, U = unknown> = (value: U) => DecoderResult<T>;
```

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):
Expand Down
4 changes: 2 additions & 2 deletions examples/type-annotations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person>'.
// 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<Person>'.
// Property 'age' is missing in type '{ name: string; aye: number; }' but required in type 'Person'.
const personDecoder2: Decoder<Person> = fieldsAuto({
name: string,
aye: number,
Expand Down
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// No `any` “leaks” when _using_ the library, though.
/* eslint-disable @typescript-eslint/no-explicit-any */

export type Decoder<T, U = unknown> = (value: U) => DecoderResult<T>;
export type Decoder<T> = (value: unknown) => DecoderResult<T>;

export type DecoderResult<T> =
| {
Expand Down
4 changes: 2 additions & 2 deletions tests/decoders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1022,9 +1022,9 @@ describe("tag", () => {
});

describe("tuple", () => {
// @ts-expect-error Argument of type '{}' is not assignable to parameter of type 'readonly Decoder<unknown, unknown>[]'.
// @ts-expect-error Argument of type '{}' is not assignable to parameter of type 'Decoder<unknown>[]'.
tuple({});
// @ts-expect-error Argument of type '(value: unknown) => number' is not assignable to parameter of type 'readonly Decoder<unknown, unknown>[]'.
// @ts-expect-error Argument of type '(value: unknown) => DecoderResult<number>' is not assignable to parameter of type 'Decoder<unknown>[]'.
tuple(number);

test("0 items", () => {
Expand Down

0 comments on commit bfd1e9c

Please sign in to comment.