Skip to content

Commit 6dd27e5

Browse files
Copilotjakebailey
andauthored
Fix panic in getRestTypeAtPosition for contextually typed function with optional and rest params (#3924)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent 7892130 commit 6dd27e5

6 files changed

Lines changed: 43 additions & 2 deletions

internal/checker/relater.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,8 +1801,12 @@ func (c *Checker) getRestTypeAtPosition(source *Signature, pos int, readonly boo
18011801
return c.createArrayType(c.getIndexedAccessType(restType, c.numberType))
18021802
}
18031803
}
1804-
types := make([]*Type, parameterCount-pos)
1805-
infos := make([]TupleElementInfo, parameterCount-pos)
1804+
length := parameterCount - pos
1805+
if length <= 0 {
1806+
return c.createTupleTypeEx(nil, nil, readonly)
1807+
}
1808+
types := make([]*Type, length)
1809+
infos := make([]TupleElementInfo, length)
18061810
for i := range types {
18071811
var flags ElementFlags
18081812
if restType == nil || i < len(types)-1 {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
contextuallyTypedFunctionOptionalAndRest.ts(1,24): error TS7006: Parameter 'a' implicitly has an 'any' type.
2+
3+
4+
==== contextuallyTypedFunctionOptionalAndRest.ts (1 errors) ====
5+
const f: () => void = (a?, ...b) => {};
6+
~~
7+
!!! error TS7006: Parameter 'a' implicitly has an 'any' type.
8+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [tests/cases/compiler/contextuallyTypedFunctionOptionalAndRest.ts] ////
2+
3+
//// [contextuallyTypedFunctionOptionalAndRest.ts]
4+
const f: () => void = (a?, ...b) => {};
5+
6+
7+
//// [contextuallyTypedFunctionOptionalAndRest.js]
8+
"use strict";
9+
const f = (a, ...b) => { };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [tests/cases/compiler/contextuallyTypedFunctionOptionalAndRest.ts] ////
2+
3+
=== contextuallyTypedFunctionOptionalAndRest.ts ===
4+
const f: () => void = (a?, ...b) => {};
5+
>f : Symbol(f, Decl(contextuallyTypedFunctionOptionalAndRest.ts, 0, 5))
6+
>a : Symbol(a, Decl(contextuallyTypedFunctionOptionalAndRest.ts, 0, 23))
7+
>b : Symbol(b, Decl(contextuallyTypedFunctionOptionalAndRest.ts, 0, 26))
8+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [tests/cases/compiler/contextuallyTypedFunctionOptionalAndRest.ts] ////
2+
3+
=== contextuallyTypedFunctionOptionalAndRest.ts ===
4+
const f: () => void = (a?, ...b) => {};
5+
>f : () => void
6+
>(a?, ...b) => {} : (a?: any) => void
7+
>a : any
8+
>b : []
9+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @strict: true
2+
3+
const f: () => void = (a?, ...b) => {};

0 commit comments

Comments
 (0)