Skip to content

Commit 0fea59f

Browse files
committed
Add spread tests
1 parent bf866ce commit 0fea59f

10 files changed

Lines changed: 270 additions & 4 deletions

tests/cases/conformance/es6/destructuring/destructuringObjectBindingPatternAndAssignment3.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@ var {h?} = { h?: 1 };
33
var {i}: string | number = { i: 2 };
44
var {i1}: string | number| {} = { i1: 2 };
55
var { f2: {f21} = { f212: "string" } }: any = undefined;
6-
var { ...d1 } = {
7-
a: 1, b: 1, d1: 9, e: 10
8-
}
96
var {1} = { 1 };
10-
var {"prop"} = { "prop": 1 };
7+
var {"prop"} = { "prop": 1 };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
interface Congealed<T, U> {
2+
...T
3+
...U
4+
}
5+
6+
let sandwich: Congealed<{jam: number }, { peanutButter: number }>;
7+
sandwich.jam;
8+
sandwich.peanutButter;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// @target: es5
2+
let o = { a: 1, b: 'no' }
3+
let o2 = { b: 'yes', c: true }
4+
let swap = { a: 'yes', b: -1 };
5+
6+
let addAfter: { a: number, b: string, c: boolean } =
7+
{ ...o, c: false }
8+
let addBefore: { a: number, b: string, c: boolean } =
9+
{ c: false, ...o }
10+
// Note: ignore still changes the order that properties are printed
11+
let ignore: { a: number, b: string } =
12+
{ b: 'ignored', ...o }
13+
let override: { a: number, b: string } =
14+
{ ...o, b: 'override' }
15+
let nested: { a: number, b: boolean, c: string } =
16+
{ ...{ a: 3, ...{ b: false, c: 'overriden' } }, c: 'whatever' }
17+
let combined: { a: number, b: string, c: boolean } =
18+
{ ...o, ...o2 }
19+
let combinedBefore: { a: number, b: string, c: boolean } =
20+
{ b: 'ok', ...o, ...o2 }
21+
let combinedMid: { a: number, b: string, c: boolean } =
22+
{ ...o, b: 'ok', ...o2 }
23+
let combinedAfter: { a: number, b: string, c: boolean } =
24+
{ ...o, ...o2, b: 'ok' }
25+
let combinedNested: { a: number, b: boolean, c: string, d: string } =
26+
{ ...{ a: 4, ...{ b: false, c: 'overriden' } }, d: 'actually new', ...{ a: 5, d: 'maybe new' } }
27+
let combinedNestedChangeType: { a: number, b: boolean, c: number } =
28+
{ ...{ a: 1, ...{ b: false, c: 'overriden' } }, c: -1 }
29+
let propertyNested: { a: { a: number, b: string } } =
30+
{ a: { ... o } }
31+
// accessors don't copy the descriptor
32+
// (which means that readonly getters become read/write properties)
33+
let op = { get a () { return 6 } };
34+
let getter: { a: number, c: number } =
35+
{ ...op, c: 7 }
36+
getter.a = 12;
37+
38+
// null and undefined are just skipped
39+
let spreadNull: { a: number } =
40+
{ a: 7, ...null }
41+
let spreadUndefined: { a: number } =
42+
{ a: 7, ...undefined }
43+
44+
// methods are not enumerable
45+
class C { p = 1; m() { } }
46+
let c: C = new C()
47+
let spreadC: { p: number } = { ...c }
48+
49+
// own methods are enumerable
50+
let cplus: { p: number, plus(): void } = { ...c, plus() { return this.p + 1; } };
51+
cplus.plus();
52+
53+
// new field's type conflicting with existing field is OK
54+
let changeTypeAfter: { a: string, b: string } =
55+
{ ...o, a: 'wrong type?' }
56+
let changeTypeBefore: { a: number, b: string } =
57+
{ a: 'wrong type?', ...o };
58+
let changeTypeBoth: { a: string, b: number } =
59+
{ ...o, ...swap };
60+
61+
// optional
62+
let definiteBoolean: { sn: boolean };
63+
let definiteString: { sn: string };
64+
let optionalString: { sn?: string };
65+
let optionalNumber: { sn?: number };
66+
let optionalUnionStops: { sn: string | number | boolean } = { ...definiteBoolean, ...definiteString, ...optionalNumber };
67+
let optionalUnionDuplicates: { sn: string | number } = { ...definiteBoolean, ...definiteString, ...optionalString, ...optionalNumber };
68+
let allOptional: { sn?: string | number } = { ...optionalString, ...optionalNumber };
69+
70+
// computed property
71+
let computedFirst: { a: number, b: string, "before everything": number } =
72+
{ ['before everything']: 12, ...o, b: 'yes' }
73+
let computedMiddle: { a: number, b: string, c: boolean, "in the middle": number } =
74+
{ ...o, ['in the middle']: 13, b: 'maybe?', ...o2 }
75+
let computedAfter: { a: number, b: string, "at the end": number } =
76+
{ ...o, b: 'yeah', ['at the end']: 14 }
77+
// shortcut syntax
78+
let a = 12;
79+
let shortCutted: { a: number, b: string } = { ...o, a }
80+
81+
// generics
82+
function f<T, U>(t: T, u: U): { id: string, ...T, ...U } {
83+
return { id: 'id', ...t, ...u };
84+
}
85+
let exclusive: { id: string, a: number, b: string, c: string, d: boolean } =
86+
f({ a: 1, b: 'yes' }, { c: 'no', d: false })
87+
let overlap: { id: string, a: number, b: string } =
88+
f({ a: 1 }, { a: 2, b: 'extra' })
89+
let overlapConflict: { id:string, a: string } =
90+
f({ a: 1 }, { a: 'mismatch' })
91+
let overwriteId: { id: boolean, a: number, c: number, d: string } =
92+
f({ a: 1, id: true }, { c: 1, d: 'no' })
93+
94+
class D { m() { }; q = 2; }
95+
let classesAreWrong: { id: string, ...C, ...D } =
96+
f(new C(), new D())
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function f<T, U, V>(t: T, u: U, v: V): void {
2+
let o: { ...T, ...U, ...V };
3+
const same: { ...T, ...U, ...V } = o; // ok
4+
const reversed: { ...V, ...U, ...T } = o; // error, reversed
5+
const reversed2: { ...U, ...T, ...V } = o; // error, U and T are still reversed
6+
const missingT: { ...U, ...V } = o; // error, missing T
7+
const missingU: { ...T, ...V } = o; // error, missing U
8+
const missingV: { ...T, ...U } = o; // error, missing V
9+
const atEnd: { ...T, ...U, second: string } = { ...t, ...u, second: 'foo' }; // ok
10+
const atBeginning: { first: string, ...T, ...U, } = { first: 'foo', ...t, ...u }; // ok
11+
12+
const emptyTarget: { } = { ...t, ...u } // ok
13+
const emptySource: { ...T, ...U } = { }; // error, {} is not assignable to U (or T)
14+
15+
let optionalNumber: { sn?: number };
16+
let optionalString: { sn?: string };
17+
let optionalBoolean: { sn?: boolean };
18+
const unionCutoff: { ...T, sn?: number | string | boolean } =
19+
{ ...optionalBoolean, ...t, ...optionalString, ...optionalNumber } // ok
20+
unionCutoff.sn; // ok
21+
const optionalCutoff = { ...t, ...optionalNumber }; // ok
22+
optionalCutoff.sn; // ok
23+
24+
const interspersed: { first: string, ...T, second: string, ...U, third: string } =
25+
{ first: '1', ...t, second: '2', ...u, third: '3' }; // ok
26+
const interspersedMissingU: { first: string, second: string, ...T, third: string } =
27+
{ first: '1', ...t, second: '2', ...u, third: '3' }; // error, 'U' is missing
28+
const interspersedOrder1: { first: string, ...T, second: string, ...U, third: string, secondsecond: string } =
29+
{ first: '1', ...t, second: '2', ...u, third: '3', secondsecond: 'false' }; // ok
30+
const interspersedOrder2: { first: string, second: string, secondsecond: string, third: string, ...T, ...U } =
31+
{ first: '1', ...t, second: '2', ...u, third: '3', secondsecond: 'false' }; // ok
32+
33+
34+
const mismatchFirst: { first: string, ...T, second: string, ...U, third: string } =
35+
{ firrrrrrst: '1', ...t, second: '2', ...u, third: '3' }; // error, 'first' not found
36+
const mismatchSecond: { first: string, ...T, second: string, ...U, third: string } =
37+
{ first: '1', ...t, ssssssssecond: '2', ...u, third: '3' }; // error, 'second' not found
38+
const mismatchLast: { first: string, ...T, second: string, ...U, third: string } =
39+
{ first: '1', ...t, second: '2', ...u, thirrrrrrrd: '3' }; // error, 'third' not found
40+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// @target: es5
2+
let o = { a: 1, b: 'no' }
3+
4+
/// private propagates
5+
class PrivateOptionalX {
6+
private x?: number;
7+
}
8+
class PublicX {
9+
public x: number;
10+
}
11+
let privateOptionalx: PrivateOptionalX;
12+
let publicx: PublicX;
13+
let o3 = { ...publicx, ...privateOptionalx };
14+
let sn: string | number = o3.x; // error, x is private
15+
let optionalString: { sn?: string };
16+
let optionalNumber: { sn?: number };
17+
let allOptional: { sn: string | number } = { ...optionalString, ...optionalNumber };
18+
// error, 'sn' is optional in source, required in target
19+
20+
// assignability as target
21+
interface Bool { b: boolean };
22+
interface Str { s: string };
23+
let spread: { ...Bool, ...Str } = { s: 'foo' }; // error, missing 'b'
24+
let b: Bool;
25+
spread = b; // error, missing 's'
26+
27+
// expressions are not allowed
28+
let o1 = { ...1 + 1 };
29+
let o2 = { ...(1 + 1) };
30+
31+
// literal repeats are not allowed, but spread repeats are fine
32+
let duplicated = { b: 'bad', ...o, b: 'bad', ...o2, b: 'bad' }
33+
let duplicatedSpread = { ...o, ...o }
34+
35+
// write-only properties get skipped
36+
let setterOnly = { ...{ set b (bad: number) { } } };
37+
setterOnly.b = 12; // error, 'b' does not exist
38+
39+
// methods are skipped because they aren't enumerable
40+
class C { p = 1; m() { } }
41+
let c: C = new C()
42+
let spreadC = { ...c }
43+
spreadC.m(); // error 'm' is not in '{ ... c }'
44+
45+
let callableConstructableSpread: { ...PublicX, (n: number): number, new (p: number) };
46+
callableConstructableSpread(12); // error, no call signature
47+
new callableConstructableSpread(12); // error, no construct signature
48+
49+
let callableSpread = { ...publicx, ...(n => n + 1) }; // error, can't spread functions
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let o7 = { ...o? };
2+
let o8 = { ...*o };
3+
let o9 = { ...matchMedia() { }};
4+
let o10 = { ...get x() { return 12; }};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// <reference path='fourslash.ts'/>
2+
////let o = { a: 1, b: 'no' }
3+
////let o2 = { b: 'yes', c: true }
4+
////let swap = { a: 'yes', b: -1 };
5+
////let addAfter: { a: number, b: string, c: boolean } =
6+
//// { ...o, c: false }
7+
////let addBefore: { a: number, b: string, c: boolean } =
8+
//// { c: false, ...o }
9+
////let ignore: { a: number, b: string } =
10+
//// { b: 'ignored', ...o }
11+
////ignore./*1*/a;
12+
////let combinedNestedChangeType: { a: number, b: boolean, c: number } =
13+
//// { ...{ a: 1, ...{ b: false, c: 'overriden' } }, c: -1 }
14+
////combinedNestedChangeType./*2*/a;
15+
////let spreadNull: { a: number } =
16+
//// { a: 7, ...null }
17+
////let spreadUndefined: { a: number } =
18+
//// { a: 7, ...undefined }
19+
////spreadNull./*3*/a;
20+
////spreadUndefined./*4*/a;
21+
goTo.marker('1');
22+
verify.memberListContains('a', '(property) a: number');
23+
verify.memberListContains('b', '(property) b: string');
24+
verify.memberListCount(2);
25+
goTo.marker('2');
26+
verify.memberListContains('a', '(property) a: number');
27+
verify.memberListContains('b', '(property) b: boolean');
28+
verify.memberListContains('c', '(property) c: number');
29+
verify.memberListCount(3);
30+
goTo.marker('3');
31+
verify.memberListContains('a', '(property) a: number');
32+
verify.memberListCount(1);
33+
goTo.marker('4');
34+
verify.memberListContains('a', '(property) a: number');
35+
verify.memberListCount(1);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////interface A1 { [|a|]: number };
4+
////interface A2 { [|a|]?: number };
5+
////let a12: { ...A1, ...A2 };
6+
////a12.[|a|];
7+
const ranges = test.ranges();
8+
// members of spread types only refer to themselves and the resulting property
9+
verify.referencesOf(ranges[0], [ranges[0], ranges[2]]);
10+
verify.referencesOf(ranges[1], [ranges[1], ranges[2]]);
11+
// but the resulting property refers to everything
12+
verify.referencesOf(ranges[2], ranges);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////interface A1 { /*1*/a: number };
4+
////interface A2 { /*2*/a?: number };
5+
////let a12: { ...A1, ...A2 };
6+
////a12.a/*3*/;
7+
verify.goToDefinition('3', [ '1', '2' ]);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////interface A1 { [|a|]: number };
4+
////interface A2 { [|a|]?: number };
5+
////let a12: { ...A1, ...A2 };
6+
////a12.[|a|];
7+
const ranges = test.ranges();
8+
verify.assertHasRanges(ranges);
9+
10+
// A1 unions with A2, so rename A1.a and a12.a
11+
goTo.position(ranges[0].start);
12+
verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false, [ranges[0], ranges[2]]);
13+
// A1 unions with A2, so rename A2.a and a12.a
14+
goTo.position(ranges[1].start);
15+
verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false, [ranges[1], ranges[2]]);
16+
// a12.a unions A1.a and A2.a, so rename A1.a, A2.a and a12.a
17+
goTo.position(ranges[2].start);
18+
verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false, [ranges[0], ranges[1], ranges[2]]);

0 commit comments

Comments
 (0)