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

Improve checking of complex rest parameter types #26676

Merged
merged 8 commits into from
Aug 29, 2018
Merged
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
198 changes: 101 additions & 97 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(13,13): error TS2370: A rest parameter must be of an array type.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(14,17): error TS1047: A rest parameter cannot be optional.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(15,16): error TS1048: A rest parameter cannot have an initializer.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(20,19): error TS2345: Argument of type 'true' is not assignable to parameter of type 'string | number'.
Expand All @@ -16,7 +15,7 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(34,28): error TS2304: Cannot find name 'E'.


==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts (11 errors) ====
==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts (10 errors) ====
// If the parameter is a rest parameter, the parameter type is any[]
// A type annotation for a rest parameter must denote an array type.

Expand All @@ -30,8 +29,6 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(
function a0(...x: [number, number, string]) { } // Error, rest parameter must be array type
function a1(...x: (number|string)[]) { }
function a2(...a: someArray) { } // Error, rest parameter must be array type
~~~~~~~~~~~~~~~
!!! error TS2370: A rest parameter must be of an array type.
function a3(...b?) { } // Error, can't be optional
~
!!! error TS1047: A rest parameter cannot be optional.
Expand Down
148 changes: 148 additions & 0 deletions tests/baselines/reference/genericRestParameters3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
tests/cases/conformance/types/rest/genericRestParameters3.ts(11,11): error TS2345: Argument of type '[10]' is not assignable to parameter of type '[string] | [number, boolean]'.
Type '[10]' is not assignable to type '[string]'.
Type '10' is not assignable to type 'string'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(12,1): error TS2345: Argument of type '[]' is not assignable to parameter of type '[string] | [number, boolean]'.
Type '[]' is not assignable to type '[number, boolean]'.
Property '0' is missing in type '[]'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(16,1): error TS2322: Type '(x: string, ...args: [string] | [number, boolean]) => void' is not assignable to type '(...args: [string, string] | [string, number, boolean]) => void'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(17,1): error TS2322: Type '(x: string, y: string) => void' is not assignable to type '(x: string, ...args: [string] | [number, boolean]) => void'.
Types of parameters 'y' and 'args' are incompatible.
Type '[string] | [number, boolean]' is not assignable to type '[string]'.
Type '[number, boolean]' is not assignable to type '[string]'.
Types of property '0' are incompatible.
Type 'number' is not assignable to type 'string'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(18,1): error TS2322: Type '(x: string, y: number, z: boolean) => void' is not assignable to type '(x: string, ...args: [string] | [number, boolean]) => void'.
Types of parameters 'y' and 'args' are incompatible.
Type '[string] | [number, boolean]' is not assignable to type '[number, boolean]'.
Type '[string]' is not assignable to type '[number, boolean]'.
Property '1' is missing in type '[string]'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(19,1): error TS2322: Type '(...args: [string, string] | [string, number, boolean]) => void' is not assignable to type '(x: string, ...args: [string] | [number, boolean]) => void'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(29,1): error TS2554: Expected 1 arguments, but got 0.
tests/cases/conformance/types/rest/genericRestParameters3.ts(30,21): error TS2345: Argument of type '100' is not assignable to parameter of type '(...args: CoolArray<any>) => void'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(31,21): error TS2345: Argument of type '<T extends any[]>(cb: (...args: T) => void) => void' is not assignable to parameter of type '(...args: CoolArray<any>) => void'.
Types of parameters 'cb' and 'args' are incompatible.
Type 'CoolArray<any>' is not assignable to type '[(...args: any[]) => void]'.
Property '0' is missing in type 'CoolArray<any>'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(38,32): error TS2345: Argument of type '[10, 20]' is not assignable to parameter of type 'CoolArray<number>'.
Property 'hello' is missing in type '[10, 20]'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(43,1): error TS2345: Argument of type '[]' is not assignable to parameter of type 'CoolArray<never>'.
Property 'hello' is missing in type '[]'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(44,5): error TS2345: Argument of type '[number]' is not assignable to parameter of type 'CoolArray<{}>'.
Property 'hello' is missing in type '[number]'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(45,5): error TS2345: Argument of type '[number, number]' is not assignable to parameter of type 'CoolArray<{}>'.
Property 'hello' is missing in type '[number, number]'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(46,5): error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'CoolArray<number>'.
Property 'hello' is missing in type 'number[]'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(53,5): error TS2345: Argument of type '["what"]' is not assignable to parameter of type '[] | [number, string]'.
Type '["what"]' is not assignable to type '[number, string]'.
Property '1' is missing in type '["what"]'.


==== tests/cases/conformance/types/rest/genericRestParameters3.ts (15 errors) ====
declare let f1: (x: string, ...args: [string] | [number, boolean]) => void;
declare let f2: (x: string, y: string) => void;
declare let f3: (x: string, y: number, z: boolean) => void;
declare let f4: (...args: [string, string] | [string, number, boolean]) => void;

declare const tt: [string] | [number, boolean];

f1("foo", "abc");
f1("foo", 10, true);
f1("foo", ...tt);
f1("foo", 10); // Error
~~
!!! error TS2345: Argument of type '[10]' is not assignable to parameter of type '[string] | [number, boolean]'.
!!! error TS2345: Type '[10]' is not assignable to type '[string]'.
!!! error TS2345: Type '10' is not assignable to type 'string'.
f1("foo"); // Error
~~~~~~~~~
!!! error TS2345: Argument of type '[]' is not assignable to parameter of type '[string] | [number, boolean]'.
!!! error TS2345: Type '[]' is not assignable to type '[number, boolean]'.
!!! error TS2345: Property '0' is missing in type '[]'.

f2 = f1;
f3 = f1;
f4 = f1; // Error, misaligned complex rest types
~~
!!! error TS2322: Type '(x: string, ...args: [string] | [number, boolean]) => void' is not assignable to type '(...args: [string, string] | [string, number, boolean]) => void'.
f1 = f2; // Error
~~
!!! error TS2322: Type '(x: string, y: string) => void' is not assignable to type '(x: string, ...args: [string] | [number, boolean]) => void'.
!!! error TS2322: Types of parameters 'y' and 'args' are incompatible.
!!! error TS2322: Type '[string] | [number, boolean]' is not assignable to type '[string]'.
!!! error TS2322: Type '[number, boolean]' is not assignable to type '[string]'.
!!! error TS2322: Types of property '0' are incompatible.
!!! error TS2322: Type 'number' is not assignable to type 'string'.
f1 = f3; // Error
~~
!!! error TS2322: Type '(x: string, y: number, z: boolean) => void' is not assignable to type '(x: string, ...args: [string] | [number, boolean]) => void'.
!!! error TS2322: Types of parameters 'y' and 'args' are incompatible.
!!! error TS2322: Type '[string] | [number, boolean]' is not assignable to type '[number, boolean]'.
!!! error TS2322: Type '[string]' is not assignable to type '[number, boolean]'.
!!! error TS2322: Property '1' is missing in type '[string]'.
f1 = f4; // Error, misaligned complex rest types
~~
!!! error TS2322: Type '(...args: [string, string] | [string, number, boolean]) => void' is not assignable to type '(x: string, ...args: [string] | [number, boolean]) => void'.

// Repro from #26110

interface CoolArray<E> extends Array<E> {
hello: number;
}

declare function foo<T extends any[]>(cb: (...args: T) => void): void;

foo<CoolArray<any>>(); // Error
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
foo<CoolArray<any>>(100); // Error
~~~
!!! error TS2345: Argument of type '100' is not assignable to parameter of type '(...args: CoolArray<any>) => void'.
foo<CoolArray<any>>(foo); // Error
~~~
!!! error TS2345: Argument of type '<T extends any[]>(cb: (...args: T) => void) => void' is not assignable to parameter of type '(...args: CoolArray<any>) => void'.
!!! error TS2345: Types of parameters 'cb' and 'args' are incompatible.
!!! error TS2345: Type 'CoolArray<any>' is not assignable to type '[(...args: any[]) => void]'.
!!! error TS2345: Property '0' is missing in type 'CoolArray<any>'.

function bar<T extends any[]>(...args: T): T {
return args;
}

let a = bar(10, 20);
let b = bar<CoolArray<number>>(10, 20); // Error
~~
!!! error TS2345: Argument of type '[10, 20]' is not assignable to parameter of type 'CoolArray<number>'.
!!! error TS2345: Property 'hello' is missing in type '[10, 20]'.

declare function baz<T>(...args: CoolArray<T>): void;
declare const ca: CoolArray<number>;

baz(); // Error
~~~~~
!!! error TS2345: Argument of type '[]' is not assignable to parameter of type 'CoolArray<never>'.
!!! error TS2345: Property 'hello' is missing in type '[]'.
baz(1); // Error
~
!!! error TS2345: Argument of type '[number]' is not assignable to parameter of type 'CoolArray<{}>'.
!!! error TS2345: Property 'hello' is missing in type '[number]'.
baz(1, 2); // Error
~
!!! error TS2345: Argument of type '[number, number]' is not assignable to parameter of type 'CoolArray<{}>'.
!!! error TS2345: Property 'hello' is missing in type '[number, number]'.
baz(...ca); // Error
~~~~~
!!! error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'CoolArray<number>'.
!!! error TS2345: Property 'hello' is missing in type 'number[]'.

// Repro from #26491

declare function hmm<A extends [] | [number, string]>(...args: A): void;
hmm(); // okay, A = []
hmm(1, "s"); // okay, A = [1, "s"]
hmm("what"); // no error? A = [] | [number, string] ?
~~~~~~
!!! error TS2345: Argument of type '["what"]' is not assignable to parameter of type '[] | [number, string]'.
!!! error TS2345: Type '["what"]' is not assignable to type '[number, string]'.
!!! error TS2345: Property '1' is missing in type '["what"]'.

106 changes: 106 additions & 0 deletions tests/baselines/reference/genericRestParameters3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//// [genericRestParameters3.ts]
declare let f1: (x: string, ...args: [string] | [number, boolean]) => void;
declare let f2: (x: string, y: string) => void;
declare let f3: (x: string, y: number, z: boolean) => void;
declare let f4: (...args: [string, string] | [string, number, boolean]) => void;

declare const tt: [string] | [number, boolean];

f1("foo", "abc");
f1("foo", 10, true);
f1("foo", ...tt);
f1("foo", 10); // Error
f1("foo"); // Error

f2 = f1;
f3 = f1;
f4 = f1; // Error, misaligned complex rest types
f1 = f2; // Error
f1 = f3; // Error
f1 = f4; // Error, misaligned complex rest types

// Repro from #26110

interface CoolArray<E> extends Array<E> {
hello: number;
}

declare function foo<T extends any[]>(cb: (...args: T) => void): void;

foo<CoolArray<any>>(); // Error
foo<CoolArray<any>>(100); // Error
foo<CoolArray<any>>(foo); // Error

function bar<T extends any[]>(...args: T): T {
return args;
}

let a = bar(10, 20);
let b = bar<CoolArray<number>>(10, 20); // Error

declare function baz<T>(...args: CoolArray<T>): void;
declare const ca: CoolArray<number>;

baz(); // Error
baz(1); // Error
baz(1, 2); // Error
baz(...ca); // Error

// Repro from #26491

declare function hmm<A extends [] | [number, string]>(...args: A): void;
hmm(); // okay, A = []
hmm(1, "s"); // okay, A = [1, "s"]
hmm("what"); // no error? A = [] | [number, string] ?


//// [genericRestParameters3.js]
"use strict";
f1("foo", "abc");
f1("foo", 10, true);
f1.apply(void 0, ["foo"].concat(tt));
f1("foo", 10); // Error
f1("foo"); // Error
f2 = f1;
f3 = f1;
f4 = f1; // Error, misaligned complex rest types
f1 = f2; // Error
f1 = f3; // Error
f1 = f4; // Error, misaligned complex rest types
foo(); // Error
foo(100); // Error
foo(foo); // Error
function bar() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return args;
}
var a = bar(10, 20);
var b = bar(10, 20); // Error
baz(); // Error
baz(1); // Error
baz(1, 2); // Error
baz.apply(void 0, ca); // Error
hmm(); // okay, A = []
hmm(1, "s"); // okay, A = [1, "s"]
hmm("what"); // no error? A = [] | [number, string] ?


//// [genericRestParameters3.d.ts]
declare let f1: (x: string, ...args: [string] | [number, boolean]) => void;
declare let f2: (x: string, y: string) => void;
declare let f3: (x: string, y: number, z: boolean) => void;
declare let f4: (...args: [string, string] | [string, number, boolean]) => void;
declare const tt: [string] | [number, boolean];
interface CoolArray<E> extends Array<E> {
hello: number;
}
declare function foo<T extends any[]>(cb: (...args: T) => void): void;
declare function bar<T extends any[]>(...args: T): T;
declare let a: [number, number];
declare let b: any;
declare function baz<T>(...args: CoolArray<T>): void;
declare const ca: CoolArray<number>;
declare function hmm<A extends [] | [number, string]>(...args: A): void;
Loading