Fix panic when inferring [...rest, ...T] from tuple shorter than fixed-arity constraint#3917
Merged
Merged
Conversation
…d-arity constraint Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/7618a648-4ae3-4585-8479-774b65e1636d Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix tsgo panics when inferring from tuple shorter than fixed-arity
Fix panic when inferring [...rest, ...T] from tuple shorter than fixed-arity constraint
May 16, 2026
7 tasks
Member
|
@copilot resolve the merge conflicts in this pull request |
…cs-inference # Conflicts: # internal/checker/inference.go Co-authored-by: ahejlsberg <4226954+ahejlsberg@users.noreply.github.com>
Contributor
Author
ahejlsberg
approved these changes
May 17, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a Go panic in the [...rest, ...T] tuple inference branch when the source tuple is shorter than the fixed-arity constraint of T. The Go-native slice c.getTypeArguments(source)[startIndex:endIndex] panics on negative startIndex, while JavaScript's Array.slice would clamp; the fix guards the slice with startIndex >= startLength before performing inference, and adds a regression compiler test.
Changes:
- Add
startIndex >= startLengthbounds check around the[...rest, ...T]trailing slice/inference block ininferFromObjectTypes. - Add
inferRestTupleShortSource.tscompiler test plus accepted baselines (.types,.symbols,.js,.errors.txt).
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| internal/checker/inference.go | Wraps trailing-slice creation and both inferFromTypes calls in a startIndex >= startLength guard to avoid negative slice bounds. |
| testdata/tests/cases/compiler/inferRestTupleShortSource.ts | New regression test reproducing the panic with f<T extends [string]>(args: [...string[], ...T]) called as f([]). |
| testdata/baselines/reference/compiler/inferRestTupleShortSource.types | Accepted types baseline showing f([]) resolves to void with no crash. |
| testdata/baselines/reference/compiler/inferRestTupleShortSource.symbols | Accepted symbols baseline for the new test. |
| testdata/baselines/reference/compiler/inferRestTupleShortSource.js | Accepted JS emit baseline for the new test. |
| testdata/baselines/reference/compiler/inferRestTupleShortSource.errors.txt | Accepted error baseline showing TS2345 with target [...string[], string] (T defaults to its constraint). |
RyanCavanaugh
approved these changes
May 18, 2026
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.
Go slices panic on negative indices, unlike JS
Array.slicewhich clamps to 0. In the[...rest, ...T]inference branch,startIndex = endIndex - impliedAritygoes negative when the source tuple is shorter thanT's constraint arity.startIndex >= startLengthbefore slicing source tuple type arguments ininferFromObjectTypes