Skip to content

Compute TypeScript type #78

@JSteunou

Description

@JSteunou

🚀 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 which allow to generate Interface or Type from a model

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

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

interface ITodo extends Instance<typeof Todo> {}
// => {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 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions