typescript-json-schema icon indicating copy to clipboard operation
typescript-json-schema copied to clipboard

Support for functions

Open 5ebastianMeier opened this issue 7 years ago • 7 comments

We'd like to generate schemas from our interfaces and also include functions and their respective parameters, as well as their corresponding types.

Do you plan to support such a scenario? In case you don't, do you have some advice where to start if we wanted to contribute that feature?

5ebastianMeier avatar May 16 '17 10:05 5ebastianMeier

I found the place in the code that needs to be extended to support methods. I'll open a pullrequest as soon as i'm done writing a getMethodDefinition-method

heikomat avatar Jun 04 '17 21:06 heikomat

I met similar problems.

Currently, typescript-json-schema just ignore merhod definition by https://github.com/YousefED/typescript-json-schema/pull/194.

export type HandlerObject = {
    prop(props: {}): string
};

This output is

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object"
}

But, a property of function not ignored.

index.ts:

export type Handler = (props: {}) => string
export type HandlerObject = {
    prop: Handler
};

Command

typescript-json-schema index.ts HandlerObject

This output is:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "properties": {
        "prop": {
            "type": "object"
        }
    },
    "type": "object"
}

Reproduce repository: https://github.com/azu/typescript-json-schema-function-object-issue

azu avatar Apr 10 '20 15:04 azu

Is this still on the radar?

mattoni avatar May 13 '20 21:05 mattoni

Not for me since I don’t think functions are supported in JSON Schema.

I’d be happy to review a proposal and we’ll tested pull request.

I reopened the issue to look into why functions are not filtered out.

domoritz avatar May 13 '20 22:05 domoritz

While including functions in the schema of a type itself doesn't really make sense, some way of making all of that type info (and TSdoc comments) available would be amazing. Specifically, I'd like to be able to see the return and param types of functions, types for static class properties, and TSDoc comments attached to functions.

I'm using this to generate API docs, and easily being able to grab the type info (and comments) for a function would let me more easily include the description and spec for request and response bodies.

I've been able to largely work around this by using a custom ts compiler transformer to add stringified typenames into reflected metadata, and then looking up those types in the generated json schema. But with this setup, I currently have to always use a named type for route params, rather than defining them inline, and I have to avoid static properties if I want type info and comments about them. It's working pretty well, but having more type info available certainly would have made things more flexible.

theoephraim avatar Jul 24 '20 23:07 theoephraim

I'm also looking into this, I need to translate the typescript type definition into another language (Dart) and I need the functions as well. If this is against the schema it would be enough to add a switch that to enable to feature only when needed

Hexer10 avatar Jul 31 '20 23:07 Hexer10

Any update on this?

havsar avatar Aug 18 '23 19:08 havsar