1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
tests/cases/conformance/types/spread/objectSpreadNegative.ts(13,21): error TS2339: Property 'x' does not exist on type '{}'.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(16,5): error TS2322: Type '{ sn?: string | number; }' is not assignable to type '{ sn: string | number; }'.
Property 'sn' is optional in type '{ sn?: string | number; }' but required in type '{ sn: string | number; }'.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(23,1): error TS2322: Type '{ s: string; }' is not assignable to type '{ s: string; b: boolean; }'.
Property 'b' is missing in type '{ s: string; }'.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(25,1): error TS2322: Type '{ b: boolean; }' is not assignable to type '{ s: string; b: boolean; }'.
Property 's' is missing in type '{ b: boolean; }'.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(28,36): error TS2300: Duplicate identifier 'b'.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(28,53): error TS2300: Duplicate identifier 'b'.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(32,19): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(33,19): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(35,20): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(37,19): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(42,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '{}' has no compatible call signatures.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(46,12): error TS2339: Property 'b' does not exist on type '{}'.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(52,9): error TS2339: Property 'm' does not exist on type '{ p: number; }'.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(56,14): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(59,14): error TS2698: Spread types may only be created from object types.
==== tests/cases/conformance/types/spread/objectSpreadNegative.ts (15 errors) ====
let o = { a: 1, b: 'no' }
/// private propagates
class PrivateOptionalX {
private x?: number;
}
class PublicX {
public x: number;
}
let publicX: PublicX;
let privateOptionalX: PrivateOptionalX;
let o2 = { ...publicX, ...privateOptionalX };
let sn: number = o2.x; // error, x is private
~
!!! error TS2339: Property 'x' does not exist on type '{}'.
let optionalString: { sn?: string };
let optionalNumber: { sn?: number };
let allOptional: { sn: string | number } = { ...optionalString, ...optionalNumber };
~~~~~~~~~~~
!!! error TS2322: Type '{ sn?: string | number; }' is not assignable to type '{ sn: string | number; }'.
!!! error TS2322: Property 'sn' is optional in type '{ sn?: string | number; }' but required in type '{ sn: string | number; }'.
// error, 'sn' is optional in source, required in target
// assignability as target
interface Bool { b: boolean };
interface Str { s: string };
let spread = { ...{ b: true }, ...{s: "foo" } };
spread = { s: "foo" }; // error, missing 'b'
~~~~~~
!!! error TS2322: Type '{ s: string; }' is not assignable to type '{ s: string; b: boolean; }'.
!!! error TS2322: Property 'b' is missing in type '{ s: string; }'.
let b = { b: false };
spread = b; // error, missing 's'
~~~~~~
!!! error TS2322: Type '{ b: boolean; }' is not assignable to type '{ s: string; b: boolean; }'.
!!! error TS2322: Property 's' is missing in type '{ b: boolean; }'.
// literal repeats are not allowed, but spread repeats are fine
let duplicated = { b: 'bad', ...o, b: 'bad', ...o2, b: 'bad' }
~
!!! error TS2300: Duplicate identifier 'b'.
~
!!! error TS2300: Duplicate identifier 'b'.
let duplicatedSpread = { ...o, ...o }
// primitives are not allowed
let spreadNum = { ...12 };
~~~~~
!!! error TS2698: Spread types may only be created from object types.
let spreadSum = { ...1 + 1 };
~~~~~~~~
!!! error TS2698: Spread types may only be created from object types.
spreadSum.toFixed(); // error, no methods from number
let spreadBool = { ...false };
~~~~~~~~
!!! error TS2698: Spread types may only be created from object types.
spreadBool.valueOf(); // error, what were you thinking?
let spreadStr = { ...'foo' };
~~~~~~~~
!!! error TS2698: Spread types may only be created from object types.
spreadStr.length; // error, no 'length'
spreadStr.charAt(1); // error, no methods either
// functions are skipped
let spreadFunc = { ...function () { } }
spreadFunc(); // error, no call signature
~~~~~~~~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '{}' has no compatible call signatures.
// write-only properties get skipped
let setterOnly = { ...{ set b (bad: number) { } } };
setterOnly.b = 12; // error, 'b' does not exist
~
!!! error TS2339: Property 'b' does not exist on type '{}'.
// methods are skipped because they aren't enumerable
class C { p = 1; m() { } }
let c: C = new C()
let spreadC = { ...c }
spreadC.m(); // error 'm' is not in '{ ... c }'
~
!!! error TS2339: Property 'm' does not exist on type '{ p: number; }'.
// generics
function f<T, U>(t: T, u: U) {
return { ...t, ...u, id: 'id' };
~~~~
!!! error TS2698: Spread types may only be created from object types.
}
function override<U>(initial: U, override: U): U {
return { ...initial, ...override };
~~~~~~~~~~
!!! error TS2698: Spread types may only be created from object types.
}
let exclusive: { id: string, a: number, b: string, c: string, d: boolean } =
f({ a: 1, b: 'yes' }, { c: 'no', d: false })
let overlap: { id: string, a: number, b: string } =
f({ a: 1 }, { a: 2, b: 'extra' })
let overlapConflict: { id:string, a: string } =
f({ a: 1 }, { a: 'mismatch' })
let overwriteId: { id: string, a: number, c: number, d: string } =
f({ a: 1, id: true }, { c: 1, d: 'no' })
|