feat: add fallback resolution for indexed access types#1831
Open
thazhemadam wants to merge 1 commit intolukeautry:masterfrom
Open
feat: add fallback resolution for indexed access types#1831thazhemadam wants to merge 1 commit intolukeautry:masterfrom
thazhemadam wants to merge 1 commit intolukeautry:masterfrom
Conversation
9ffc9e7 to
b7dafdb
Compare
Indexed access patterns like `T[keyof U], MappedType[Union]`, and `(typeof x)[keyof typeof y]` previously threw "Unknown type: IndexedAccessType". Add a fallback that delegates to TypeScript's type checker to evaluate any indexed access type the compiler accepts, then recursively resolves the resulting simpler type node. In the fallback, ensure that we prevent infinite recursion when `typeToTypeNode` returns another `IndexedAccessTypeNode`, e.g. generic-dependent patterns like `DM[T]`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Indexed access patterns like
T[keyof U], MappedType[Union], and(typeof x)[keyof typeof y]previously threw"Unknown type: IndexedAccessType".
Add a fallback that delegates to TypeScript's type checker to evaluate any indexed access type the compiler accepts, then recursively resolves the resulting simpler type node.
All Submissions:
Closing issues
Closes #1622.
If this is a new feature submission:
Potential Problems With The Approach
This doesn't fix all forms of the
IndexedAccessTypeNodeerrors.For instance, generic-dependent indexed access types (e.g., as in #1375) are not resolved by this change. In such cases, we bail out to avoid infinite recursion and throw the same error as before.
Test plan
Positive test cases:
ForeignIndexedValue:(typeof x)[keyof typeof y]wherexandyare different objects. Asserts the schema resolves to{ type: "string", enum: ["FOO"] }.EventUnion:{ [K in Union]: ... }[Union], a mapped type indexed by a union alias. Asserts the schema resolves toanyOfwith 2 object variants, each containing the expected properties.ConfigValue:ConfigMap[ConfigKey]whereConfigKey = keyof ConfigMap(GenerateMetadataError: Unknown type: IndexedAccessType #1622 pattern). Asserts the schema resolves to anyOf with 2 variants (the heterogeneous value types of the interface).One negative test case that documents the known limitation:
GenericIndexed<PT.C>:DetailMap[T]whereTis a generic type parameter (GenerateMetadataError: Unknown type: IndexedAccessType #1375 pattern). Asserts that an error is cleanly thrown, rather than stack overflowing.