Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get return type from ArrowFunction #1887

Open
marviobezerra opened this issue Mar 13, 2024 · 2 comments
Open

Get return type from ArrowFunction #1887

marviobezerra opened this issue Mar 13, 2024 · 2 comments

Comments

@marviobezerra
Copy link

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
Copy link
Member

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
Copy link
Member

I upgraded function support significantly in #1910. Please send a pull request to add return values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants