Skip to content

Commit fc306ba

Browse files
authored
Merge pull request #15849 from Microsoft/allow-spread-after-required-params
Allow spreading arrays after required parameters
2 parents ecbfdbf + cefcc66 commit fc306ba

File tree

4 files changed

+186
-1
lines changed

4 files changed

+186
-1
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14760,7 +14760,7 @@ namespace ts {
1476014760
// If spread arguments are present, check that they correspond to a rest parameter. If so, no
1476114761
// further checking is necessary.
1476214762
if (spreadArgIndex >= 0) {
14763-
return isRestParameterIndex(signature, spreadArgIndex);
14763+
return isRestParameterIndex(signature, spreadArgIndex) || spreadArgIndex >= signature.minArgumentCount;
1476414764
}
1476514765

1476614766
// Too many arguments implies incorrect arity.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(30,5): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
2+
Type 'string' is not assignable to type 'number'.
3+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(31,5): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
4+
Type 'string' is not assignable to type 'number'.
5+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(32,13): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
6+
Type 'string' is not assignable to type 'number'.
7+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(33,13): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
8+
Type 'string' is not assignable to type 'number'.
9+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(34,11): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
10+
Type 'string' is not assignable to type 'number'.
11+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(35,11): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
12+
Type 'string' is not assignable to type 'number'.
13+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(36,1): error TS2346: Supplied parameters do not match any signature of call target.
14+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(37,1): error TS2346: Supplied parameters do not match any signature of call target.
15+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(38,1): error TS2346: Supplied parameters do not match any signature of call target.
16+
17+
18+
==== tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts (9 errors) ====
19+
declare function all(a?: number, b?: number): void;
20+
declare function weird(a?: number | string, b?: number | string): void;
21+
declare function prefix(s: string, a?: number, b?: number): void;
22+
declare function rest(s: string, a?: number, b?: number, ...rest: number[]): void;
23+
declare function normal(s: string): void;
24+
declare function thunk(): string;
25+
26+
declare var ns: number[];
27+
declare var mixed: (number | string)[];
28+
declare var tuple: [number, string];
29+
30+
// good
31+
all(...ns)
32+
weird(...ns)
33+
weird(...mixed)
34+
weird(...tuple)
35+
prefix("a", ...ns)
36+
rest("d", ...ns)
37+
38+
39+
// this covers the arguments case
40+
normal("g", ...ns)
41+
normal("h", ...mixed)
42+
normal("i", ...tuple)
43+
thunk(...ns)
44+
thunk(...mixed)
45+
thunk(...tuple)
46+
47+
// bad
48+
all(...mixed)
49+
~~~~~~~~
50+
!!! error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
51+
!!! error TS2345: Type 'string' is not assignable to type 'number'.
52+
all(...tuple)
53+
~~~~~~~~
54+
!!! error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
55+
!!! error TS2345: Type 'string' is not assignable to type 'number'.
56+
prefix("b", ...mixed)
57+
~~~~~~~~
58+
!!! error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
59+
!!! error TS2345: Type 'string' is not assignable to type 'number'.
60+
prefix("c", ...tuple)
61+
~~~~~~~~
62+
!!! error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
63+
!!! error TS2345: Type 'string' is not assignable to type 'number'.
64+
rest("e", ...mixed)
65+
~~~~~~~~
66+
!!! error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
67+
!!! error TS2345: Type 'string' is not assignable to type 'number'.
68+
rest("f", ...tuple)
69+
~~~~~~~~
70+
!!! error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
71+
!!! error TS2345: Type 'string' is not assignable to type 'number'.
72+
prefix(...ns) // required parameters are required
73+
~~~~~~~~~~~~~
74+
!!! error TS2346: Supplied parameters do not match any signature of call target.
75+
prefix(...mixed)
76+
~~~~~~~~~~~~~~~~
77+
!!! error TS2346: Supplied parameters do not match any signature of call target.
78+
prefix(...tuple)
79+
~~~~~~~~~~~~~~~~
80+
!!! error TS2346: Supplied parameters do not match any signature of call target.
81+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//// [callWithSpread2.ts]
2+
declare function all(a?: number, b?: number): void;
3+
declare function weird(a?: number | string, b?: number | string): void;
4+
declare function prefix(s: string, a?: number, b?: number): void;
5+
declare function rest(s: string, a?: number, b?: number, ...rest: number[]): void;
6+
declare function normal(s: string): void;
7+
declare function thunk(): string;
8+
9+
declare var ns: number[];
10+
declare var mixed: (number | string)[];
11+
declare var tuple: [number, string];
12+
13+
// good
14+
all(...ns)
15+
weird(...ns)
16+
weird(...mixed)
17+
weird(...tuple)
18+
prefix("a", ...ns)
19+
rest("d", ...ns)
20+
21+
22+
// this covers the arguments case
23+
normal("g", ...ns)
24+
normal("h", ...mixed)
25+
normal("i", ...tuple)
26+
thunk(...ns)
27+
thunk(...mixed)
28+
thunk(...tuple)
29+
30+
// bad
31+
all(...mixed)
32+
all(...tuple)
33+
prefix("b", ...mixed)
34+
prefix("c", ...tuple)
35+
rest("e", ...mixed)
36+
rest("f", ...tuple)
37+
prefix(...ns) // required parameters are required
38+
prefix(...mixed)
39+
prefix(...tuple)
40+
41+
42+
//// [callWithSpread2.js]
43+
// good
44+
all.apply(void 0, ns);
45+
weird.apply(void 0, ns);
46+
weird.apply(void 0, mixed);
47+
weird.apply(void 0, tuple);
48+
prefix.apply(void 0, ["a"].concat(ns));
49+
rest.apply(void 0, ["d"].concat(ns));
50+
// this covers the arguments case
51+
normal.apply(void 0, ["g"].concat(ns));
52+
normal.apply(void 0, ["h"].concat(mixed));
53+
normal.apply(void 0, ["i"].concat(tuple));
54+
thunk.apply(void 0, ns);
55+
thunk.apply(void 0, mixed);
56+
thunk.apply(void 0, tuple);
57+
// bad
58+
all.apply(void 0, mixed);
59+
all.apply(void 0, tuple);
60+
prefix.apply(void 0, ["b"].concat(mixed));
61+
prefix.apply(void 0, ["c"].concat(tuple));
62+
rest.apply(void 0, ["e"].concat(mixed));
63+
rest.apply(void 0, ["f"].concat(tuple));
64+
prefix.apply(void 0, ns); // required parameters are required
65+
prefix.apply(void 0, mixed);
66+
prefix.apply(void 0, tuple);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
declare function all(a?: number, b?: number): void;
2+
declare function weird(a?: number | string, b?: number | string): void;
3+
declare function prefix(s: string, a?: number, b?: number): void;
4+
declare function rest(s: string, a?: number, b?: number, ...rest: number[]): void;
5+
declare function normal(s: string): void;
6+
declare function thunk(): string;
7+
8+
declare var ns: number[];
9+
declare var mixed: (number | string)[];
10+
declare var tuple: [number, string];
11+
12+
// good
13+
all(...ns)
14+
weird(...ns)
15+
weird(...mixed)
16+
weird(...tuple)
17+
prefix("a", ...ns)
18+
rest("d", ...ns)
19+
20+
21+
// this covers the arguments case
22+
normal("g", ...ns)
23+
normal("h", ...mixed)
24+
normal("i", ...tuple)
25+
thunk(...ns)
26+
thunk(...mixed)
27+
thunk(...tuple)
28+
29+
// bad
30+
all(...mixed)
31+
all(...tuple)
32+
prefix("b", ...mixed)
33+
prefix("c", ...tuple)
34+
rest("e", ...mixed)
35+
rest("f", ...tuple)
36+
prefix(...ns) // required parameters are required
37+
prefix(...mixed)
38+
prefix(...tuple)

0 commit comments

Comments
 (0)