Merge pull request #13558 from HerringtonDarkholme/rest-spread-intrinsic
Fix #13556: enable rest/spread on `object`
This commit is contained in:
commit
ceb5fac343
11 changed files with 69 additions and 8 deletions
|
@ -1,4 +1,4 @@
|
|||
/// <reference path="moduleNameResolver.ts"/>
|
||||
/// <reference path="moduleNameResolver.ts"/>
|
||||
/// <reference path="binder.ts"/>
|
||||
|
||||
/* @internal */
|
||||
|
@ -6311,6 +6311,9 @@ namespace ts {
|
|||
if (right.flags & TypeFlags.Union) {
|
||||
return mapType(right, t => getSpreadType(left, t));
|
||||
}
|
||||
if (right.flags & TypeFlags.NonPrimitive) {
|
||||
return emptyObjectType;
|
||||
}
|
||||
|
||||
const members = createMap<Symbol>();
|
||||
const skippedPrivateMembers = createMap<boolean>();
|
||||
|
@ -11804,7 +11807,7 @@ namespace ts {
|
|||
}
|
||||
|
||||
function isValidSpreadType(type: Type): boolean {
|
||||
return !!(type.flags & (TypeFlags.Any | TypeFlags.Null | TypeFlags.Undefined) ||
|
||||
return !!(type.flags & (TypeFlags.Any | TypeFlags.Null | TypeFlags.Undefined | TypeFlags.NonPrimitive) ||
|
||||
type.flags & TypeFlags.Object && !isGenericMappedType(type) ||
|
||||
type.flags & TypeFlags.UnionOrIntersection && !forEach((<UnionOrIntersectionType>type).types, t => !isValidSpreadType(t)));
|
||||
}
|
||||
|
|
|
@ -1,10 +1,16 @@
|
|||
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAccessProperty.ts(3,3): error TS2339: Property 'nonExist' does not exist on type 'object'.
|
||||
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAccessProperty.ts(5,7): error TS2459: Type 'object' has no property 'destructuring' and no string index signature.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveAccessProperty.ts (1 errors) ====
|
||||
==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveAccessProperty.ts (2 errors) ====
|
||||
var a: object;
|
||||
a.toString();
|
||||
a.nonExist(); // error
|
||||
~~~~~~~~
|
||||
!!! error TS2339: Property 'nonExist' does not exist on type 'object'.
|
||||
|
||||
var { destructuring } = a; // error
|
||||
~~~~~~~~~~~~~
|
||||
!!! error TS2459: Type 'object' has no property 'destructuring' and no string index signature.
|
||||
var { ...rest } = a; // ok
|
||||
|
|
@ -2,9 +2,23 @@
|
|||
var a: object;
|
||||
a.toString();
|
||||
a.nonExist(); // error
|
||||
|
||||
var { destructuring } = a; // error
|
||||
var { ...rest } = a; // ok
|
||||
|
||||
|
||||
//// [nonPrimitiveAccessProperty.js]
|
||||
var __rest = (this && this.__rest) || function (s, e) {
|
||||
var t = {};
|
||||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
||||
t[p] = s[p];
|
||||
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
||||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
|
||||
t[p[i]] = s[p[i]];
|
||||
return t;
|
||||
};
|
||||
var a;
|
||||
a.toString();
|
||||
a.nonExist(); // error
|
||||
var destructuring = a.destructuring; // error
|
||||
var rest = __rest(a, []); // ok
|
||||
|
|
|
@ -78,7 +78,8 @@ let computedAfter: { a: number, b: string, "at the end": number } =
|
|||
// shortcut syntax
|
||||
let a = 12;
|
||||
let shortCutted: { a: number, b: string } = { ...o, a }
|
||||
|
||||
// non primitive
|
||||
let spreadNonPrimitive = { ...<object>{}};
|
||||
|
||||
|
||||
//// [objectSpread.js]
|
||||
|
@ -148,4 +149,6 @@ var computedAfter = __assign({}, o, (_c = { b: 'yeah' }, _c['at the end'] = 14,
|
|||
// shortcut syntax
|
||||
var a = 12;
|
||||
var shortCutted = __assign({}, o, { a: a });
|
||||
// non primitive
|
||||
var spreadNonPrimitive = __assign({}, {});
|
||||
var _a, _b, _c;
|
||||
|
|
|
@ -316,4 +316,7 @@ let shortCutted: { a: number, b: string } = { ...o, a }
|
|||
>o : Symbol(o, Decl(objectSpread.ts, 0, 3))
|
||||
>a : Symbol(a, Decl(objectSpread.ts, 78, 51))
|
||||
|
||||
// non primitive
|
||||
let spreadNonPrimitive = { ...<object>{}};
|
||||
>spreadNonPrimitive : Symbol(spreadNonPrimitive, Decl(objectSpread.ts, 80, 3))
|
||||
|
||||
|
|
|
@ -407,4 +407,10 @@ let shortCutted: { a: number, b: string } = { ...o, a }
|
|||
>o : { a: number; b: string; }
|
||||
>a : number
|
||||
|
||||
// non primitive
|
||||
let spreadNonPrimitive = { ...<object>{}};
|
||||
>spreadNonPrimitive : {}
|
||||
>{ ...<object>{}} : {}
|
||||
><object>{} : object
|
||||
>{} : {}
|
||||
|
||||
|
|
|
@ -14,11 +14,12 @@ tests/cases/conformance/types/spread/objectSpreadNegative.ts(37,19): error TS269
|
|||
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(57,11): error TS2339: Property 'a' does not exist on type '{}'.
|
||||
tests/cases/conformance/types/spread/objectSpreadNegative.ts(61,14): error TS2698: Spread types may only be created from object types.
|
||||
tests/cases/conformance/types/spread/objectSpreadNegative.ts(64,14): error TS2698: Spread types may only be created from object types.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/spread/objectSpreadNegative.ts (15 errors) ====
|
||||
==== tests/cases/conformance/types/spread/objectSpreadNegative.ts (16 errors) ====
|
||||
let o = { a: 1, b: 'no' }
|
||||
|
||||
/// private propagates
|
||||
|
@ -101,6 +102,13 @@ tests/cases/conformance/types/spread/objectSpreadNegative.ts(59,14): error TS269
|
|||
~
|
||||
!!! error TS2339: Property 'm' does not exist on type '{ p: number; }'.
|
||||
|
||||
// non primitive
|
||||
let obj: object = { a: 123 };
|
||||
let spreadObj = { ...obj };
|
||||
spreadObj.a; // error 'a' is not in {}
|
||||
~
|
||||
!!! error TS2339: Property 'a' does not exist on type '{}'.
|
||||
|
||||
// generics
|
||||
function f<T, U>(t: T, u: U) {
|
||||
return { ...t, ...u, id: 'id' };
|
||||
|
|
|
@ -52,6 +52,11 @@ let c: C = new C()
|
|||
let spreadC = { ...c }
|
||||
spreadC.m(); // error 'm' is not in '{ ... c }'
|
||||
|
||||
// non primitive
|
||||
let obj: object = { a: 123 };
|
||||
let spreadObj = { ...obj };
|
||||
spreadObj.a; // error 'a' is not in {}
|
||||
|
||||
// generics
|
||||
function f<T, U>(t: T, u: U) {
|
||||
return { ...t, ...u, id: 'id' };
|
||||
|
@ -132,6 +137,10 @@ var C = (function () {
|
|||
var c = new C();
|
||||
var spreadC = __assign({}, c);
|
||||
spreadC.m(); // error 'm' is not in '{ ... c }'
|
||||
// non primitive
|
||||
var obj = { a: 123 };
|
||||
var spreadObj = __assign({}, obj);
|
||||
spreadObj.a; // error 'a' is not in {}
|
||||
// generics
|
||||
function f(t, u) {
|
||||
return __assign({}, t, u, { id: 'id' });
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
var a: object;
|
||||
a.toString();
|
||||
a.nonExist(); // error
|
||||
|
||||
var { destructuring } = a; // error
|
||||
var { ...rest } = a; // ok
|
||||
|
|
|
@ -78,4 +78,5 @@ let computedAfter: { a: number, b: string, "at the end": number } =
|
|||
// shortcut syntax
|
||||
let a = 12;
|
||||
let shortCutted: { a: number, b: string } = { ...o, a }
|
||||
|
||||
// non primitive
|
||||
let spreadNonPrimitive = { ...<object>{}};
|
||||
|
|
|
@ -52,6 +52,11 @@ let c: C = new C()
|
|||
let spreadC = { ...c }
|
||||
spreadC.m(); // error 'm' is not in '{ ... c }'
|
||||
|
||||
// non primitive
|
||||
let obj: object = { a: 123 };
|
||||
let spreadObj = { ...obj };
|
||||
spreadObj.a; // error 'a' is not in {}
|
||||
|
||||
// generics
|
||||
function f<T, U>(t: T, u: U) {
|
||||
return { ...t, ...u, id: 'id' };
|
||||
|
|
Loading…
Reference in a new issue