typescript-json-schema
typescript-json-schema copied to clipboard
Support for functions
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?
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
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
Is this still on the radar?
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.
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.
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
Any update on this?