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

feat: basic support for inferrable types

Open swnf opened this issue 3 years ago • 1 comments

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.

swnf avatar Sep 15 '22 14:09 swnf

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).

swnf avatar Sep 17 '22 13:09 swnf

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.

domoritz avatar Oct 16 '22 13:10 domoritz

@swnf would you be able to revise this pull request?

domoritz avatar Oct 16 '22 13:10 domoritz

Yes, I think I can update it next week.

swnf avatar Oct 16 '22 14:10 swnf

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.

swnf avatar Oct 17 '22 10:10 swnf

I merged https://github.com/vega/ts-json-schema-generator/pull/1386

domoritz avatar Oct 17 '22 13:10 domoritz

I've rebased the PR. I think it is now ready for review.

swnf avatar Oct 17 '22 13:10 swnf

@domoritz can you have a look at this PR? It would help me with some eslint issues.

swnf avatar Nov 25 '22 09:11 swnf

:rocket: PR was released in v1.2.0 :rocket:

github-actions[bot] avatar Dec 22 '22 17:12 github-actions[bot]