TypeScript/tests/cases/conformance/types/spread/objectSpreadNegative.ts

54 lines
1.6 KiB
TypeScript
Raw Normal View History

2016-09-26 18:33:08 +02:00
// @target: es5
let o = { a: 1, b: 'no' }
/// private propagates
class PrivateOptionalX {
private x?: number;
}
class PublicX {
public x: number;
}
let o3: { ...PublicX, ...PrivateOptionalX };
let sn: number = o3.x; // error, x is private
2016-09-26 18:33:08 +02:00
let optionalString: { sn?: string };
let optionalNumber: { sn?: number };
let allOptional: { sn: string | number } = { ...optionalString, ...optionalNumber };
// error, 'sn' is optional in source, required in target
// assignability as target
interface Bool { b: boolean };
interface Str { s: string };
let spread: { ...Bool, ...Str } = { s: 'foo' }; // error, missing 'b'
let b: Bool;
spread = b; // error, missing 's'
// expressions are not allowed
let o1 = { ...1 + 1 };
let o2 = { ...(1 + 1) };
// literal repeats are not allowed, but spread repeats are fine
let duplicated = { b: 'bad', ...o, b: 'bad', ...o2, b: 'bad' }
let duplicatedSpread = { ...o, ...o }
// write-only properties get skipped
let setterOnly = { ...{ set b (bad: number) { } } };
setterOnly.b = 12; // error, 'b' does not exist
// 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 }'
let callableConstructableSpread: { ...PublicX, (n: number): number, new (p: number) };
callableConstructableSpread(12); // error, no call signature
new callableConstructableSpread(12); // error, no construct signature
let publicx: PublicX;
2016-09-26 18:33:08 +02:00
let callableSpread = { ...publicx, ...(n => n + 1) }; // error, can't spread functions
2016-10-04 00:49:23 +02:00
// { ...U } is not assignable to U
function override<U>(initial: U, override: U): U {
return { ...initial, ...override };
}