ts-json-schema-generator icon indicating copy to clipboard operation
ts-json-schema-generator copied to clipboard

Get return type from ArrowFunction

Open marviobezerra opened this issue 1 year ago • 3 comments

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"
    }
  }
}

marviobezerra avatar Mar 13 '24 15:03 marviobezerra