-
Notifications
You must be signed in to change notification settings - Fork 191
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
feat: support inferred types in conditionals #1265
Conversation
Fixed failing unit test.
Inner type can be ArrayType, InferType, or TupleType.
Currently only supports rest type as final target member.
Fix evaluation of nested rest types in tuples.
Fixes compiler errors on newer tsc versions.
@domoritz please let me know if there is anything else I need to do regarding this pull request. |
Hi @daanboer, for me the schema generation of a recursive Partial does not work correctly.
The generated schema for
The array items are now of type I'm not too sure what's going wrong here but I have found that I never hit your changed code in ConditionalTypeNodeParser because the following condition is always true. ts-json-schema-generator/src/NodeParser/ConditionalTypeNodeParser.ts Lines 22 to 25 in 1658e10
|
Hi @akuntsch, Nice find! There was indeed a bug where the inferred types where not passed to the subcontext when type MappedType<T> = {[K in keyof T]?: T[K]};
export type StringType = MappedType<string>; Which produces: {
"$ref": "#/definitions/StringType",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"StringType": {
"additionalProperties": false,
"type": "object"
}
}
} I think you can create a new issue for this bug (or check for an existing one)! |
Thank you for the pr! Looks great. |
🚀 PR was released in |
Fixes #513, #1075
The
infer
keyword is used in conditional expressions to infer types of type parameters. It is the basis of many useful type constructs. This pull request implements support for inferred types, including inferred rest types.The newly added
InferType
is a simple type that stores the name of the inferred parameter. Evaluation of inferred types happens inConditionalTypeNodeParser
, as type inference can only occur in conditional expressions. The heavy lifting is done by theisAssignableTo
function. It has been updated to build a map of all inferred types. These inferred types are then passed to the sub-context when evaluating the true branch. Additionally, theRestType
has been updated to not only support theArrayType
, but alsoTupleType
andInferType
.See the added tests for examples.