ts-json-schema-generator
ts-json-schema-generator copied to clipboard
feat: basic support for inferrable types
This PR adds basic support to infer the type of properties without an explicit type (see #1406). For example:
export class Foo {
a = false;
}
Previously, properties without an explicit type were always ignored. With this PR they will get their correct type, unless they are initialized with a function call or a property access. In this case they will be ignored.
I created a small helper method to create the BaseType instead of using the childNodeParser because I didn't want to add more nodes to the NodeParser that are not "type AST".
Edit: Should I just run the type checker on the member? I don't see any advantage in parsing the initializer expression's AST.
Thanks for your feedback @arthurfiorette. I made some changes based on it. However, I'm not so sure anymore if my approach makes sense. There does not seem to be any advantage in parsing the AST of the initializer (except maybe for an AsExpression). It might make more sense to just use the type checker directly to get the type of a member if it has no explicit type (would probably depend on #1386).
Thanks for the pull request. I think using the type checker directly would make sense if it makes the code a lot simpler. We mainly use the AST to preserve aliases but that isn't an issue here with inferred types.
@swnf would you be able to revise this pull request?
Yes, I think I can update it next week.
I've updated my code. It now uses the type checker and works for all types. I will rebase the PR once #1386 is merged.
I merged https://github.com/vega/ts-json-schema-generator/pull/1386
I've rebased the PR. I think it is now ready for review.
@domoritz can you have a look at this PR? It would help me with some eslint issues.
:rocket: PR was released in v1.2.0 :rocket: