joanllenas / ts.data.json

1.7kB JSON decoding library for Typescript
BSD 3-Clause "New" or "Revised" License
205 stars 16 forks source link

Typescript helper function to extract decoder type #31

Closed lanwin closed 3 years ago

lanwin commented 3 years ago

The current example looks like this.

type User = {
  firstname: string;
  lastname: string;
};

const userDecoder = JsonDecoder.object<User>(
  {
    firstname: JsonDecoder.string,
    lastname: JsonDecoder.string
  },
  'User'
);

But shouldnt it possbile to write something like this?:

const userDecoder = JsonDecoder.object(
  {
    firstname: JsonDecoder.string,
    lastname: JsonDecoder.string
  },
  'User'
);
type User = ExtractDecoderType<typeof userDecoder>;

This way I dont not have to repeat myself.

joanllenas commented 3 years ago

Hey @lanwin, that's a nice feature to have :) I'll add it in the next release.

Thanks!

joanllenas commented 3 years ago

This is ready! https://github.com/joanllenas/ts.data.json/releases/tag/v2.1.0

lanwin commented 3 years ago

Hu that went fast. Thank you!