|
| 1 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(7,34): error TS1265: A rest element cannot follow another rest element. |
| 2 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(8,34): error TS1266: An optional element cannot follow a rest element. |
| 3 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(9,30): error TS1257: A required element cannot follow an optional element. |
| 4 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(42,1): error TS2322: Type '[string, string, number, number]' is not assignable to type '[...string[], number]'. |
| 5 | + Type at positions 0 through 2 in source is not compatible with type at position 0 in target. |
| 6 | + Type 'string | number' is not assignable to type 'string'. |
| 7 | + Type 'number' is not assignable to type 'string'. |
| 8 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(48,5): error TS2345: Argument of type '["abc", "def", 5, 6]' is not assignable to parameter of type '[...strs: string[], num: number]'. |
| 9 | + Type at positions 0 through 2 in source is not compatible with type at position 0 in target. |
| 10 | + Type 'string | number' is not assignable to type 'string'. |
| 11 | + Type 'number' is not assignable to type 'string'. |
| 12 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(51,1): error TS2322: Type '[number]' is not assignable to type '[number, ...string[], number]'. |
| 13 | + Source has 1 element(s) but target requires 2. |
| 14 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(53,1): error TS2322: Type '[number, number, number]' is not assignable to type '[number, ...string[], number]'. |
| 15 | + Type at position 1 in source is not compatible with type at position 1 in target. |
| 16 | + Type 'number' is not assignable to type 'string'. |
| 17 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(56,1): error TS2322: Type '[number, string, number, string]' is not assignable to type '[number, ...string[], number]'. |
| 18 | + Type at positions 1 through 2 in source is not compatible with type at position 1 in target. |
| 19 | + Type 'string | number' is not assignable to type 'string'. |
| 20 | + Type 'number' is not assignable to type 'string'. |
| 21 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(57,8): error TS2322: Type 'boolean' is not assignable to type 'number'. |
| 22 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(58,1): error TS2322: Type '[number, string, string, boolean]' is not assignable to type '[number, ...string[], number]'. |
| 23 | + Type at position 3 in source is not compatible with type at position 2 in target. |
| 24 | + Type 'boolean' is not assignable to type 'number'. |
| 25 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(61,1): error TS2345: Argument of type '[]' is not assignable to parameter of type '[...strs: string[], n2: number]'. |
| 26 | + Source has 0 element(s) but target requires 1. |
| 27 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(63,8): error TS2345: Argument of type '[1, 2]' is not assignable to parameter of type '[...strs: string[], n2: number]'. |
| 28 | + Type at position 0 in source is not compatible with type at position 0 in target. |
| 29 | + Type 'number' is not assignable to type 'string'. |
| 30 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(66,8): error TS2345: Argument of type '["abc", 1, "def"]' is not assignable to parameter of type '[...strs: string[], n2: number]'. |
| 31 | + Type at positions 0 through 1 in source is not compatible with type at position 0 in target. |
| 32 | + Type 'string | number' is not assignable to type 'string'. |
| 33 | + Type 'number' is not assignable to type 'string'. |
| 34 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(67,5): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'number'. |
| 35 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(68,8): error TS2345: Argument of type '["abc", "def", true]' is not assignable to parameter of type '[...strs: string[], n2: number]'. |
| 36 | + Type at position 2 in source is not compatible with type at position 1 in target. |
| 37 | + Type 'boolean' is not assignable to type 'number'. |
| 38 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(71,5): error TS2322: Type '[number, number]' is not assignable to type '[number, ...T]'. |
| 39 | + Source provides no match for variadic element at position 1 in target. |
| 40 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(72,5): error TS2322: Type '[number, ...number[]]' is not assignable to type '[number, ...T]'. |
| 41 | + Target requires 2 element(s) but source may have fewer. |
| 42 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(73,5): error TS2322: Type '[number, ...T]' is not assignable to type '[number, number]'. |
| 43 | + Variadic element at position 1 in source does not match element at position 1 in target. |
| 44 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(74,5): error TS2322: Type '[number, ...T]' is not assignable to type '[number, ...number[]]'. |
| 45 | + Type at position 1 in source is not compatible with type at position 1 in target. |
| 46 | + Type 'T[number]' is not assignable to type 'number'. |
| 47 | + Type 'unknown' is not assignable to type 'number'. |
| 48 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(107,16): error TS2345: Argument of type '[1, 2, 3, 4]' is not assignable to parameter of type '[...number[], (...values: number[]) => void]'. |
| 49 | + Type at position 3 in source is not compatible with type at position 1 in target. |
| 50 | + Type 'number' is not assignable to type '(...values: number[]) => void'. |
| 51 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(108,6): error TS2345: Argument of type 'string[]' is not assignable to parameter of type '[...string[], (...values: string[]) => void]'. |
| 52 | + Source provides no match for required element at position 1 in target. |
| 53 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(111,5): error TS2345: Argument of type '[]' is not assignable to parameter of type '[...unknown[], unknown, unknown]'. |
| 54 | + Source has 0 element(s) but target requires 2. |
| 55 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(112,5): error TS2345: Argument of type '[number]' is not assignable to parameter of type '[...unknown[], unknown, unknown]'. |
| 56 | + Source has 1 element(s) but target requires 2. |
| 57 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(117,5): error TS2345: Argument of type '[]' is not assignable to parameter of type '[unknown, ...unknown[], unknown]'. |
| 58 | + Source has 0 element(s) but target requires 2. |
| 59 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(118,5): error TS2345: Argument of type '[number]' is not assignable to parameter of type '[unknown, ...unknown[], unknown]'. |
| 60 | + Source has 1 element(s) but target requires 2. |
| 61 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(128,16): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. |
| 62 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(129,16): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. |
| 63 | +tests/cases/conformance/types/tuple/variadicTuples2.ts(130,25): error TS2345: Argument of type '["blah2", 1, 2, 3]' is not assignable to parameter of type '[...string[], number]'. |
| 64 | + Type at positions 0 through 2 in source is not compatible with type at position 0 in target. |
| 65 | + Type 'string | number' is not assignable to type 'string'. |
| 66 | + Type 'number' is not assignable to type 'string'. |
| 67 | + |
| 68 | + |
| 69 | +==== tests/cases/conformance/types/tuple/variadicTuples2.ts (28 errors) ==== |
| 70 | + // Declarations |
| 71 | + |
| 72 | + type V00 = [number, ...string[]]; |
| 73 | + type V01 = [...string[], number]; |
| 74 | + type V03 = [number, ...string[], number]; |
| 75 | + |
| 76 | + type V10 = [number, ...string[], ...boolean[]]; // Error |
| 77 | + ~~~~~~~~~~~~ |
| 78 | +!!! error TS1265: A rest element cannot follow another rest element. |
| 79 | + type V11 = [number, ...string[], boolean?]; // Error |
| 80 | + ~~~~~~~~ |
| 81 | +!!! error TS1266: An optional element cannot follow a rest element. |
| 82 | + type V12 = [number, string?, boolean]; // Error |
| 83 | + ~~~~~~~ |
| 84 | +!!! error TS1257: A required element cannot follow an optional element. |
| 85 | + |
| 86 | + // Normalization |
| 87 | + |
| 88 | + type Tup3<T extends unknown[], U extends unknown[], V extends unknown[]> = [...T, ...U, ...V]; |
| 89 | + |
| 90 | + type V20 = Tup3<[number], string[], [number]>; // [number, ...string[], number] |
| 91 | + type V21 = Tup3<[number], [string?], [boolean]>; // [number, string | undefined, boolean] |
| 92 | + type V22 = Tup3<[number], string[], boolean[]>; // [number, (string | boolean)[]] |
| 93 | + type V23 = Tup3<[number], string[], [boolean?]>; // [number, (string | boolean | undefined)[]] |
| 94 | + type V24 = Tup3<[number], [boolean?], string[]>; // [number, boolean?, ...string[]] |
| 95 | + type V25 = Tup3<string[], number[], boolean[]>; // (string | number | boolean)[] |
| 96 | + type V26 = Tup3<string[], number[], [boolean]>; // [...(string | number)[], boolean] |
| 97 | + type V27 = Tup3<[number?], [string], [boolean?]>; // [number | undefined, string, boolean?] |
| 98 | + |
| 99 | + type V30<A extends unknown[]> = Tup3<A, string[], number[]>; // [...A, ...(string | number)[]] |
| 100 | + type V31<A extends unknown[]> = Tup3<string[], A, number[]>; // (string | number | A[number])[] |
| 101 | + type V32<A extends unknown[]> = Tup3<string[], number[], A>; // [...(string | number)[], ...A] |
| 102 | + |
| 103 | + type V40<A extends unknown[]> = Tup3<A, [string?], number[]>; // [...A, string?, ...number[]] |
| 104 | + type V41<A extends unknown[]> = Tup3<[string?], A, number[]>; // [string?, ...A, ...number[]] |
| 105 | + type V42<A extends unknown[]> = Tup3<[string?], number[], A>; // [string?, ...number[], ...A] |
| 106 | + |
| 107 | + type V50<A extends unknown[]> = Tup3<A, string[], [number?]>; // [...A, ...(string | number | undefined)[]] |
| 108 | + type V51<A extends unknown[]> = Tup3<string[], A, [number?]>; // (string | number | A[number] | undefined)[] |
| 109 | + type V52<A extends unknown[]> = Tup3<string[], [number?], A>; // [...(string | number | undefined)[], ...A] |
| 110 | + |
| 111 | + // Assignability |
| 112 | + |
| 113 | + declare let tt1: [...string[], number]; |
| 114 | + tt1 = [5]; |
| 115 | + tt1 = ['abc', 5]; |
| 116 | + tt1 = ['abc', 'def', 5]; |
| 117 | + tt1 = ['abc', 'def', 5, 6]; // Error |
| 118 | + ~~~ |
| 119 | +!!! error TS2322: Type '[string, string, number, number]' is not assignable to type '[...string[], number]'. |
| 120 | +!!! error TS2322: Type at positions 0 through 2 in source is not compatible with type at position 0 in target. |
| 121 | +!!! error TS2322: Type 'string | number' is not assignable to type 'string'. |
| 122 | +!!! error TS2322: Type 'number' is not assignable to type 'string'. |
| 123 | + |
| 124 | + declare function ft1(...args: [...strs: string[], num: number]): void; |
| 125 | + ft1(5); |
| 126 | + ft1('abc', 5); |
| 127 | + ft1('abc', 'def', 5); |
| 128 | + ft1('abc', 'def', 5, 6); // Error |
| 129 | + ~~~~~~~~~~~~~~~~~~ |
| 130 | +!!! error TS2345: Argument of type '["abc", "def", 5, 6]' is not assignable to parameter of type '[...strs: string[], num: number]'. |
| 131 | +!!! error TS2345: Type at positions 0 through 2 in source is not compatible with type at position 0 in target. |
| 132 | +!!! error TS2345: Type 'string | number' is not assignable to type 'string'. |
| 133 | +!!! error TS2345: Type 'number' is not assignable to type 'string'. |
| 134 | + |
| 135 | + declare let tt2: [number, ...string[], number]; |
| 136 | + tt2 = [0]; // Error |
| 137 | + ~~~ |
| 138 | +!!! error TS2322: Type '[number]' is not assignable to type '[number, ...string[], number]'. |
| 139 | +!!! error TS2322: Source has 1 element(s) but target requires 2. |
| 140 | + tt2 = [0, 1]; |
| 141 | + tt2 = [0, 1, 2]; // Error |
| 142 | + ~~~ |
| 143 | +!!! error TS2322: Type '[number, number, number]' is not assignable to type '[number, ...string[], number]'. |
| 144 | +!!! error TS2322: Type at position 1 in source is not compatible with type at position 1 in target. |
| 145 | +!!! error TS2322: Type 'number' is not assignable to type 'string'. |
| 146 | + tt2 = [0, 'abc', 1]; |
| 147 | + tt2 = [0, 'abc', 'def', 1]; |
| 148 | + tt2 = [0, 'abc', 1, 'def']; // Error |
| 149 | + ~~~ |
| 150 | +!!! error TS2322: Type '[number, string, number, string]' is not assignable to type '[number, ...string[], number]'. |
| 151 | +!!! error TS2322: Type at positions 1 through 2 in source is not compatible with type at position 1 in target. |
| 152 | +!!! error TS2322: Type 'string | number' is not assignable to type 'string'. |
| 153 | +!!! error TS2322: Type 'number' is not assignable to type 'string'. |
| 154 | + tt2 = [true, 'abc', 'def', 1]; // Error |
| 155 | + ~~~~ |
| 156 | +!!! error TS2322: Type 'boolean' is not assignable to type 'number'. |
| 157 | + tt2 = [0, 'abc', 'def', true]; // Error |
| 158 | + ~~~ |
| 159 | +!!! error TS2322: Type '[number, string, string, boolean]' is not assignable to type '[number, ...string[], number]'. |
| 160 | +!!! error TS2322: Type at position 3 in source is not compatible with type at position 2 in target. |
| 161 | +!!! error TS2322: Type 'boolean' is not assignable to type 'number'. |
| 162 | + |
| 163 | + declare function ft2(n1: number, ...rest: [...strs: string[], n2: number]): void; |
| 164 | + ft2(0); // Error |
| 165 | + ~~~~~~ |
| 166 | +!!! error TS2345: Argument of type '[]' is not assignable to parameter of type '[...strs: string[], n2: number]'. |
| 167 | +!!! error TS2345: Source has 0 element(s) but target requires 1. |
| 168 | + ft2(0, 1); |
| 169 | + ft2(0, 1, 2); // Error |
| 170 | + ~~~~ |
| 171 | +!!! error TS2345: Argument of type '[1, 2]' is not assignable to parameter of type '[...strs: string[], n2: number]'. |
| 172 | +!!! error TS2345: Type at position 0 in source is not compatible with type at position 0 in target. |
| 173 | +!!! error TS2345: Type 'number' is not assignable to type 'string'. |
| 174 | + ft2(0, 'abc', 1); |
| 175 | + ft2(0, 'abc', 'def', 1); |
| 176 | + ft2(0, 'abc', 1, 'def'); // Error |
| 177 | + ~~~~~~~~~~~~~~~ |
| 178 | +!!! error TS2345: Argument of type '["abc", 1, "def"]' is not assignable to parameter of type '[...strs: string[], n2: number]'. |
| 179 | +!!! error TS2345: Type at positions 0 through 1 in source is not compatible with type at position 0 in target. |
| 180 | +!!! error TS2345: Type 'string | number' is not assignable to type 'string'. |
| 181 | +!!! error TS2345: Type 'number' is not assignable to type 'string'. |
| 182 | + ft2(true, 'abc', 'def', 1); // Error |
| 183 | + ~~~~ |
| 184 | +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'number'. |
| 185 | + ft2(0, 'abc', 'def', true); // Error |
| 186 | + ~~~~~~~~~~~~~~~~~~ |
| 187 | +!!! error TS2345: Argument of type '["abc", "def", true]' is not assignable to parameter of type '[...strs: string[], n2: number]'. |
| 188 | +!!! error TS2345: Type at position 2 in source is not compatible with type at position 1 in target. |
| 189 | +!!! error TS2345: Type 'boolean' is not assignable to type 'number'. |
| 190 | + |
| 191 | + function ft3<T extends unknown[]>(x: [number, ...T], y: [number, number], z: [number, ...number[]]) { |
| 192 | + x = y; // Error |
| 193 | + ~ |
| 194 | +!!! error TS2322: Type '[number, number]' is not assignable to type '[number, ...T]'. |
| 195 | +!!! error TS2322: Source provides no match for variadic element at position 1 in target. |
| 196 | + x = z; // Error |
| 197 | + ~ |
| 198 | +!!! error TS2322: Type '[number, ...number[]]' is not assignable to type '[number, ...T]'. |
| 199 | +!!! error TS2322: Target requires 2 element(s) but source may have fewer. |
| 200 | + y = x; // Error |
| 201 | + ~ |
| 202 | +!!! error TS2322: Type '[number, ...T]' is not assignable to type '[number, number]'. |
| 203 | +!!! error TS2322: Variadic element at position 1 in source does not match element at position 1 in target. |
| 204 | + z = x; // Error |
| 205 | + ~ |
| 206 | +!!! error TS2322: Type '[number, ...T]' is not assignable to type '[number, ...number[]]'. |
| 207 | +!!! error TS2322: Type at position 1 in source is not compatible with type at position 1 in target. |
| 208 | +!!! error TS2322: Type 'T[number]' is not assignable to type 'number'. |
| 209 | +!!! error TS2322: Type 'unknown' is not assignable to type 'number'. |
| 210 | + } |
| 211 | + |
| 212 | + // Inference |
| 213 | + |
| 214 | + function pipe<T extends readonly unknown[]>(...args: [...T, (...values: T) => void]) { |
| 215 | + const callback = args[args.length - 1] as (...values: T) => void; |
| 216 | + const values = args.slice(0, -1) as unknown as T; |
| 217 | + callback(...values); |
| 218 | + } |
| 219 | + |
| 220 | + pipe("foo", 123, true, (a, b, c) => { |
| 221 | + a; // string |
| 222 | + b; // number |
| 223 | + c; // boolean |
| 224 | + }) |
| 225 | + |
| 226 | + pipe("foo", 123, true, (...x) => { |
| 227 | + x; // [string, number, boolean] |
| 228 | + }); |
| 229 | + |
| 230 | + declare const sa: string[]; |
| 231 | + |
| 232 | + pipe(...sa, (...x) => { |
| 233 | + x; // string[] |
| 234 | + }); |
| 235 | + |
| 236 | + pipe(1, ...sa, 2, (...x) => { |
| 237 | + x; // [number, ...string[], number] |
| 238 | + let qq = x[x.length - 1]; |
| 239 | + let ww = x[0] |
| 240 | + }); |
| 241 | + |
| 242 | + pipe<number[]>(1, 2, 3, 4); // Error |
| 243 | + ~~~~~~~~~~ |
| 244 | +!!! error TS2345: Argument of type '[1, 2, 3, 4]' is not assignable to parameter of type '[...number[], (...values: number[]) => void]'. |
| 245 | +!!! error TS2345: Type at position 3 in source is not compatible with type at position 1 in target. |
| 246 | +!!! error TS2345: Type 'number' is not assignable to type '(...values: number[]) => void'. |
| 247 | + pipe(...sa); // Error |
| 248 | + ~~~~~ |
| 249 | +!!! error TS2345: Argument of type 'string[]' is not assignable to parameter of type '[...string[], (...values: string[]) => void]'. |
| 250 | +!!! error TS2345: Source provides no match for required element at position 1 in target. |
| 251 | + |
| 252 | + declare function fn1<T, U>(t: [...unknown[], T, U]): [T, U]; |
| 253 | + fn1([]); // Error |
| 254 | + ~~ |
| 255 | +!!! error TS2345: Argument of type '[]' is not assignable to parameter of type '[...unknown[], unknown, unknown]'. |
| 256 | +!!! error TS2345: Source has 0 element(s) but target requires 2. |
| 257 | + fn1([1]); // Error |
| 258 | + ~~~ |
| 259 | +!!! error TS2345: Argument of type '[number]' is not assignable to parameter of type '[...unknown[], unknown, unknown]'. |
| 260 | +!!! error TS2345: Source has 1 element(s) but target requires 2. |
| 261 | + fn1([1, 'abc']); // [number, string] |
| 262 | + fn1([1, 'abc', true]); // [string, boolean] |
| 263 | + |
| 264 | + declare function fn2<T, U>(t: [T, ...unknown[], U]): [T, U]; |
| 265 | + fn2([]); // Error |
| 266 | + ~~ |
| 267 | +!!! error TS2345: Argument of type '[]' is not assignable to parameter of type '[unknown, ...unknown[], unknown]'. |
| 268 | +!!! error TS2345: Source has 0 element(s) but target requires 2. |
| 269 | + fn2([1]); // Error |
| 270 | + ~~~ |
| 271 | +!!! error TS2345: Argument of type '[number]' is not assignable to parameter of type '[unknown, ...unknown[], unknown]'. |
| 272 | +!!! error TS2345: Source has 1 element(s) but target requires 2. |
| 273 | + fn2([1, 'abc']); // [number, string] |
| 274 | + fn2([1, 'abc', true]); // [number, boolean] |
| 275 | + |
| 276 | + // Repro from #39595 |
| 277 | + |
| 278 | + declare function foo<S extends readonly [string, ...string[]]>(...stringsAndNumber: readonly [...S, number]): [...S, number]; |
| 279 | + |
| 280 | + const a1 = foo('blah1', 1); |
| 281 | + const b1 = foo('blah1', 'blah2', 1); |
| 282 | + const c1 = foo(1); // Error |
| 283 | + ~ |
| 284 | +!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. |
| 285 | + const d1 = foo(1, 2); // Error |
| 286 | + ~ |
| 287 | +!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. |
| 288 | + const e1 = foo('blah1', 'blah2', 1, 2, 3); // Error |
| 289 | + ~~~~~~~~~~~~~~~~ |
| 290 | +!!! error TS2345: Argument of type '["blah2", 1, 2, 3]' is not assignable to parameter of type '[...string[], number]'. |
| 291 | +!!! error TS2345: Type at positions 0 through 2 in source is not compatible with type at position 0 in target. |
| 292 | +!!! error TS2345: Type 'string | number' is not assignable to type 'string'. |
| 293 | +!!! error TS2345: Type 'number' is not assignable to type 'string'. |
| 294 | + |
0 commit comments