fastify / fastify/fluent-json-schema

Compute TypeScript type

Open
#78 10 comments 15 reactions 0 assignees View on GitHub
enhancement
Dominant language
JavaScript
Stars
526
Forks
58
PR merge metrics
No merged PRs in 30d

Description

## 🚀 Feature Proposal

I think it will be possible and convenient to add a way to expose the TypeScript type of each object schema

## Motivation

I started, like the doc of fastify suggest, to use `fluent-schema` (which is awesome!) and `json-schema-to-typescript` to generate types... in separate files.

Also this method could be ok for those who prefer having separate files, separate schema & types, I am not a big fan of depending on a script to re-generate those types. And even with a banner saying *do not modify this file, update the schema instead*, this could be error prone.

Another way to handle the case would be to add the capacity to `fluent-schema` to generate types directly. It would allow users to export a schema and the matching interface or type from the same module, not depending on scripts, not having several files, not having a type written that can be modified, not having another library, ... You got the point, it would be just simpler :)

## Example

To my opinion, a good example of API for this case is [mobx-state-tree](https://mobx-state-tree.js.org/tips/typescript) which allow to generate Interface or Type from a model

```js
import {Instance} from 'mobx-state-tree'

const Todo = types
.model({
title: "hello",
done: false,
})

interface ITodo extends Instance {}
// => {title: string, done: boolean}

type TTodo = typeof Todo.Type
// => {title: string, done: boolean}
```

The helper `Instance` is from MobX State Tree here, not a TypeScript helper.

The bad news is, I don't know how to do this myself, but I can learn and help if needed :pray:

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.