Skip to content
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

consider spread tuples when matching signature #18004

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7223,6 +7223,17 @@ namespace ts {
return links.resolvedType;
}

function getTupleTypeElementTypes(type: Type): Type[] {
Debug.assert(isTupleLikeType(type));
const types = [];
let idx = 0;
let symbol: Symbol;
while (symbol = getPropertyOfObjectType(type, idx++ + "" as __String)) {
types.push(getTypeOfSymbol(symbol));
}
return types;
}

interface TypeSet extends Array<Type> {
containsAny?: boolean;
containsUndefined?: boolean;
Expand Down Expand Up @@ -15482,7 +15493,28 @@ namespace ts {
return node.attributes.properties.length > 0 ? [node.attributes] : emptyArray;
}
else {
return node.arguments || emptyArray;
return flatMap(node.arguments || emptyArray, (arg: Expression) => {
if (arg.kind !== SyntaxKind.SpreadElement) {
return arg;
}
const spread = (arg as SpreadElement).expression;
if (spread.kind === SyntaxKind.ArrayLiteralExpression) {
return (spread as ArrayLiteralExpression).elements;
}
const type = getApparentType(checkExpression(spread));
if (isTupleLikeType(type)) {
const types = getTupleTypeElementTypes(type);
// hack to make expression nodes for the elements: create element access nodes
return map(types, (el: Type, i: number) => {
const elNode = el && createElementAccess(spread, i);
elNode.parent = spread.parent;
return elNode;
});
}
else {
return arg;
}
});
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,9 @@ namespace ts {
}

export function getErrorSpanForNode(sourceFile: SourceFile, node: Node): TextSpan {
if (node.pos < 0) {
return getErrorSpanForNode(sourceFile, node.parent);
}
let errorNode = node;
switch (node.kind) {
case SyntaxKind.SourceFile:
Expand Down
32 changes: 16 additions & 16 deletions tests/baselines/reference/callWithSpread2.errors.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(24,1): error TS2554: Expected 1 arguments, but got 3.
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(27,1): error TS2554: Expected 0 arguments, but got 2.
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(30,5): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(31,5): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(31,5): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(32,13): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(33,13): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(33,13): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(34,11): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(35,11): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(35,11): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(36,1): error TS2556: Expected 1-3 arguments, but got a minimum of 0.
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(37,1): error TS2556: Expected 1-3 arguments, but got a minimum of 0.
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(38,1): error TS2556: Expected 1-3 arguments, but got a minimum of 0.
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(38,8): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.


==== tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts (9 errors) ====
==== tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts (11 errors) ====
declare function all(a?: number, b?: number): void;
declare function weird(a?: number | string, b?: number | string): void;
declare function prefix(s: string, a?: number, b?: number): void;
Expand All @@ -40,9 +39,13 @@ tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(38,1): erro
normal("g", ...ns)
normal("h", ...mixed)
normal("i", ...tuple)
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 3.
thunk(...ns)
thunk(...mixed)
thunk(...tuple)
~~~~~~~~~~~~~~~
!!! error TS2554: Expected 0 arguments, but got 2.

// bad
all(...mixed)
Expand All @@ -51,31 +54,28 @@ tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(38,1): erro
!!! error TS2345: Type 'string' is not assignable to type 'number'.
all(...tuple)
~~~~~~~~
!!! error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
!!! error TS2345: Type 'string' is not assignable to type 'number'.
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
prefix("b", ...mixed)
~~~~~~~~
!!! error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
!!! error TS2345: Type 'string' is not assignable to type 'number'.
prefix("c", ...tuple)
~~~~~~~~
!!! error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
!!! error TS2345: Type 'string' is not assignable to type 'number'.
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
rest("e", ...mixed)
~~~~~~~~
!!! error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
!!! error TS2345: Type 'string' is not assignable to type 'number'.
rest("f", ...tuple)
~~~~~~~~
!!! error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
!!! error TS2345: Type 'string' is not assignable to type 'number'.
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
prefix(...ns) // required parameters are required
~~~~~~~~~~~~~
!!! error TS2556: Expected 1-3 arguments, but got a minimum of 0.
prefix(...mixed)
~~~~~~~~~~~~~~~~
!!! error TS2556: Expected 1-3 arguments, but got a minimum of 0.
prefix(...tuple)
~~~~~~~~~~~~~~~~
!!! error TS2556: Expected 1-3 arguments, but got a minimum of 0.
~~~~~~~~
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.

37 changes: 37 additions & 0 deletions tests/baselines/reference/iteratorSpreadInCall12.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
tests/cases/conformance/es6/spread/iteratorSpreadInCall12.ts(31,36): error TS2345: Argument of type 'string' is not assignable to parameter of type 'symbol'.


==== tests/cases/conformance/es6/spread/iteratorSpreadInCall12.ts (1 errors) ====
class Foo<T> {
constructor(...s: T[]) { }
}

class SymbolIterator {
next() {
return {
value: Symbol(),
done: false
};
}

[Symbol.iterator]() {
return this;
}
}

class StringIterator {
next() {
return {
value: "",
done: false
};
}

[Symbol.iterator]() {
return this;
}
}

new Foo(...[...new SymbolIterator, ...[...new StringIterator]]);
~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'symbol'.
4 changes: 2 additions & 2 deletions tests/baselines/reference/iteratorSpreadInCall9.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/conformance/es6/spread/iteratorSpreadInCall9.ts(31,32): error TS2345: Argument of type 'string' is not assignable to parameter of type 'symbol'.
tests/cases/conformance/es6/spread/iteratorSpreadInCall9.ts(31,36): error TS2345: Argument of type 'string' is not assignable to parameter of type 'symbol'.


==== tests/cases/conformance/es6/spread/iteratorSpreadInCall9.ts (1 errors) ====
Expand Down Expand Up @@ -33,6 +33,6 @@ tests/cases/conformance/es6/spread/iteratorSpreadInCall9.ts(31,32): error TS2345
}

new Foo(...new SymbolIterator, ...[...new StringIterator]);
~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'symbol'.

4 changes: 2 additions & 2 deletions tests/baselines/reference/keyofAndIndexedAccess.types
Original file line number Diff line number Diff line change
Expand Up @@ -1717,8 +1717,8 @@ function f1(thing: Thing) {
>'b' : "b"

let x4 = path(thing, ...['a', 'x']); // any
>x4 : any
>path(thing, ...['a', 'x']) : any
>x4 : number
>path(thing, ...['a', 'x']) : number
>path : { <T, K1 extends keyof T>(obj: T, key1: K1): T[K1]; <T, K1 extends keyof T, K2 extends keyof T[K1]>(obj: T, key1: K1, key2: K2): T[K1][K2]; <T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(obj: T, key1: K1, key2: K2, key3: K3): T[K1][K2][K3]; (obj: any, ...keys: (string | number)[]): any; }
>thing : Thing
>...['a', 'x'] : string
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/spreadTuples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [spreadTuples.ts]
function foo(x: number, y: number, z: number) {}
var args: [0, 1, 2] = [0, 1, 2];
foo(...args);


//// [spreadTuples.js]
function foo(x, y, z) { }
var args = [0, 1, 2];
foo.apply(void 0, args);
14 changes: 14 additions & 0 deletions tests/baselines/reference/spreadTuples.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/spreadTuples.ts ===
function foo(x: number, y: number, z: number) {}
>foo : Symbol(foo, Decl(spreadTuples.ts, 0, 0))
>x : Symbol(x, Decl(spreadTuples.ts, 0, 13))
>y : Symbol(y, Decl(spreadTuples.ts, 0, 23))
>z : Symbol(z, Decl(spreadTuples.ts, 0, 34))

var args: [0, 1, 2] = [0, 1, 2];
>args : Symbol(args, Decl(spreadTuples.ts, 1, 3))

foo(...args);
>foo : Symbol(foo, Decl(spreadTuples.ts, 0, 0))
>args : Symbol(args, Decl(spreadTuples.ts, 1, 3))

20 changes: 20 additions & 0 deletions tests/baselines/reference/spreadTuples.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/compiler/spreadTuples.ts ===
function foo(x: number, y: number, z: number) {}
>foo : (x: number, y: number, z: number) => void
>x : number
>y : number
>z : number

var args: [0, 1, 2] = [0, 1, 2];
>args : [0, 1, 2]
>[0, 1, 2] : [0, 1, 2]
>0 : 0
>1 : 1
>2 : 2

foo(...args);
>foo(...args) : void
>foo : (x: number, y: number, z: number) => void
>...args : 0 | 1 | 2
>args : [0, 1, 2]

3 changes: 3 additions & 0 deletions tests/cases/compiler/spreadTuples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function foo(x: number, y: number, z: number) {}
var args: [0, 1, 2] = [0, 1, 2];
foo(...args);