Fix panic in getRestTypeAtPosition for contextually typed function with optional and rest params#3924
Open
Copilot wants to merge 2 commits into
Open
Fix panic in getRestTypeAtPosition for contextually typed function with optional and rest params#3924Copilot wants to merge 2 commits into
Copilot wants to merge 2 commits into
Conversation
When a contextually typed function has more parameters than the context
type (e.g. `const f: () => void = (a?, ...b) => {};`), pos can exceed
parameterCount, making the slice length negative and causing a panic.
Add a bounds check to return an empty tuple when parameterCount - pos <= 0,
matching the TypeScript reference implementation behavior.
Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/740d6ea1-5b03-4627-9bc9-a887a0bd62d1
Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix tsgo panics for contextually typed function with optional and rest params
Fix panic in getRestTypeAtPosition for contextually typed function with optional and rest params
May 16, 2026
jakebailey
approved these changes
May 16, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a panic in the checker when computing a rest type at a parameter position beyond the contextual signature’s parameter count (e.g., context signature has 0 params but pos is 1), by avoiding negative slice lengths and returning an empty tuple rest type instead.
Changes:
- Add a guard in
Checker.getRestTypeAtPositionto return an empty tuple whenparameterCount - pos <= 0. - Add a compiler test that reproduces the crash scenario (contextually typed function expression with optional + rest parameters).
- Add new reference baselines for the test (types/symbols/js/errors).
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/relater.go |
Prevents make([]T, negative) by handling out-of-range pos with an empty tuple rest type. |
testdata/tests/cases/compiler/contextuallyTypedFunctionOptionalAndRest.ts |
Adds a minimal repro case for the panic scenario. |
testdata/baselines/reference/compiler/contextuallyTypedFunctionOptionalAndRest.types |
Captures expected type output for the new test. |
testdata/baselines/reference/compiler/contextuallyTypedFunctionOptionalAndRest.symbols |
Captures expected symbol output for the new test. |
testdata/baselines/reference/compiler/contextuallyTypedFunctionOptionalAndRest.js |
Captures expected JS emit for the new test. |
testdata/baselines/reference/compiler/contextuallyTypedFunctionOptionalAndRest.errors.txt |
Captures expected diagnostics for the new test. |
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.
getRestTypeAtPositionpanics withmakeslice: len out of rangewhen the source signature has fewer parameters thanpos, producing a negative slice length.The context signature has 0 parameters, but
assignContextualParameterTypespassespos=1(the non-rest param count of the function expression). The TS reference uses dynamic arrays with afor (let i = pos; i < parameterCount; i++)loop that naturally handles this—Go's pre-allocatedmakedoes not.getRestTypeAtPosition: whenparameterCount - pos <= 0, return an empty tuple