-
Notifications
You must be signed in to change notification settings - Fork 0
feat: extend indexed array access support #5
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect results for mixed-element tuples; return union only when all elements are string literals.
If a tuple mixes string literals with other types (e.g.,
[1, "a"]), the current logic returns a union of only the string literals and silently drops the rest, producing an unsound schema. You should either:Indexbehavior unless every element is a string literal, orAt minimum, gate on “all elements are string literals.”
Apply this diff to bail unless all elements are string literals and to support named tuple members:
private extractTupleUnion(typeNode: Node | undefined): ts.Expression | null { if (!typeNode) return null let actualTupleType: Node | undefined = typeNode // Handle readonly modifier (TypeOperator) if (typeNode.isKind(ts.SyntaxKind.TypeOperator)) { const typeOperator = typeNode.asKindOrThrow(ts.SyntaxKind.TypeOperator) actualTupleType = typeOperator.getTypeNode() } // Check if it's a tuple type if (actualTupleType?.isKind(ts.SyntaxKind.TupleType)) { const tupleType = actualTupleType.asKindOrThrow(ts.SyntaxKind.TupleType) const elements = tupleType.getElements() - // Extract literal types from tuple elements - const literalTypes: ts.Expression[] = [] - for (const element of elements) { - if (element.isKind(ts.SyntaxKind.LiteralType)) { - const literalTypeNode = element.asKindOrThrow(ts.SyntaxKind.LiteralType) - const literal = literalTypeNode.getLiteral() - - if (literal.isKind(ts.SyntaxKind.StringLiteral)) { - const stringLiteral = literal.asKindOrThrow(ts.SyntaxKind.StringLiteral) - const value = stringLiteral.getLiteralValue() - literalTypes.push(makeTypeCall('Literal', [ts.factory.createStringLiteral(value)])) - } - } - } - - // Return union of literal types if we found any - if (literalTypes.length > 0) { - return makeTypeCall('Union', [ts.factory.createArrayLiteralExpression(literalTypes)]) - } + // Extract literal string types from tuple elements. + // Bail out if any element is not a string literal. + const literalTypes: ts.Expression[] = [] + for (const element of elements) { + // Support named tuple members: [x: "a"] + const candidate = + element.isKind(ts.SyntaxKind.NamedTupleMember) ? element.getTypeNode() : element + if (!candidate || !candidate.isKind(ts.SyntaxKind.LiteralType)) { + return null + } + const literal = candidate.asKindOrThrow(ts.SyntaxKind.LiteralType).getLiteral() + if (!literal.isKind(ts.SyntaxKind.StringLiteral)) { + return null + } + const value = literal.asKindOrThrow(ts.SyntaxKind.StringLiteral).getLiteralValue() + literalTypes.push(makeTypeCall('Literal', [ts.factory.createStringLiteral(value)])) + } + + // Only return a union if every element contributed a string literal + if (literalTypes.length === elements.length && literalTypes.length > 0) { + return makeTypeCall('Union', [ts.factory.createArrayLiteralExpression(literalTypes)]) + } } return null }📝 Committable suggestion
🤖 Prompt for AI Agents