Thursday 21 October 2021

Is it possible to output simplified Typescript types in the compiled distribution of a Node module

I am trying to create more simplified output in the *.d.ts typescript files which are included with the bundled version of my modules code. These files are generated through a chain involving the typescript compiler, babel, and rollup. I am also using relay and graphql, but I don't think that will impact the question much.

As an example, in the .tsx source code, I have something like this:

import { graphql } from 'babel-plugin-relay/macro'

// produces some generated code 
graphql`fragment task_model on Task { id message createdAt deadline modifiers { hard medium easy } }`

// There is a generated type for this fragment, but it is a little ugly
import { task_model } from './__generated__/task.graphql'

// We can "correct" the type information and export it with a nicer name
export type Task = Readonly<Omit<task_model & { deadline: string, createdAt: string }, ' $refType'>>

In the .d.ts output, the file says:

export declare type Task = Readonly<Omit<task_model & {
    deadline: string;
    createdAt: string;
}, ' $refType'>>;

But, within a capable IDE, I can see that type resolves to a more sensible

type Task = {
    readonly id: string;
    readonly message: string;
    readonly createdAt: string;
    readonly deadline: string;
    readonly modifiers: {
        readonly hard: number;
        readonly medium: number;
        readonly easy: number;
    };
}

Is if there is some option or some way to output the cleaner human readable type resolution, instead of the version which describes the corrections? I think it would be nice to be able to understand the type information even if the project using the module is not properly configured to use it.



from Is it possible to output simplified Typescript types in the compiled distribution of a Node module

No comments:

Post a Comment