vega / ts-json-schema-generator

Generate JSON schema from your Typescript sources
MIT License
1.38k stars 190 forks source link

Get return type from ArrowFunction #1887

Open marviobezerra opened 4 months ago

marviobezerra commented 4 months ago

I want to get the return type of an arrow function. Is it possible?

Here is my sample code

export const mySample = () => {
    const result = {
        name: 'Foo',
        today: new Date()
    }

    return result;
}

export type MySampleType = ReturnType<typeof mySample>;

I'm using a simple configuration as following

import { createGenerator } from "ts-json-schema-generator";

const config = {
    path: "./src/sample.ts",
    tsconfig: "./tsconfig.json",
    type: "*",
};

const schema = createGenerator(config)
    .createSchema(config.type);

console.log(JSON.stringify(schema, null, 2));

It prints this output

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "NamedParameters<typeof mySample>": {
      "type": "object",
      "additionalProperties": false
    },
    "MySampleType": {}
  }
}

I was expecting to get something like this.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "NamedParameters<typeof mySample>": {
      "type": "object",
      "additionalProperties": false
    },
    "MySampleType": {
        "name": "string",
        "today" "date"
    }
  }
}
domoritz commented 4 months ago

You code looks reasonable and it would be nice if this worked. Looks like there is a bug somewhere. Can you help fix it?

domoritz commented 3 months ago

I upgraded function support significantly in https://github.com/vega/ts-json-schema-generator/pull/1910. Please send a pull request to add return values.