Skip to content

Commit 3631af6

Browse files
authored
Remove readonly from object rest properties (microsoft#23746)
* Remove readonly from object rest properties Works the same as removing it from object spread properties * Fix bug number in test
1 parent d5ef117 commit 3631af6

File tree

5 files changed

+138
-1
lines changed

5 files changed

+138
-1
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4180,7 +4180,7 @@ namespace ts {
41804180
const isPrivate = getDeclarationModifierFlagsFromSymbol(prop) & (ModifierFlags.Private | ModifierFlags.Protected);
41814181
const isSetOnlyAccessor = prop.flags & SymbolFlags.SetAccessor && !(prop.flags & SymbolFlags.GetAccessor);
41824182
if (!inNamesToRemove && !isPrivate && !isClassMethod(prop) && !isSetOnlyAccessor) {
4183-
members.set(prop.escapedName, prop);
4183+
members.set(prop.escapedName, getNonReadonlySymbol(prop));
41844184
}
41854185
}
41864186
const stringIndexInfo = getIndexInfoOfType(source, IndexKind.String);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [objectRestReadonly.ts]
2+
// #23734
3+
type ObjType = {
4+
foo: string
5+
baz: string
6+
quux: string
7+
}
8+
9+
const obj: Readonly<ObjType> = {
10+
foo: 'bar',
11+
baz: 'qux',
12+
quux: 'quuz',
13+
}
14+
15+
const { foo, ...rest } = obj
16+
17+
delete rest.baz
18+
19+
20+
//// [objectRestReadonly.js]
21+
var __rest = (this && this.__rest) || function (s, e) {
22+
var t = {};
23+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
24+
t[p] = s[p];
25+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
26+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
27+
t[p[i]] = s[p[i]];
28+
return t;
29+
};
30+
var obj = {
31+
foo: 'bar',
32+
baz: 'qux',
33+
quux: 'quuz'
34+
};
35+
var foo = obj.foo, rest = __rest(obj, ["foo"]);
36+
delete rest.baz;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
=== tests/cases/conformance/types/rest/objectRestReadonly.ts ===
2+
// #23734
3+
type ObjType = {
4+
>ObjType : Symbol(ObjType, Decl(objectRestReadonly.ts, 0, 0))
5+
6+
foo: string
7+
>foo : Symbol(foo, Decl(objectRestReadonly.ts, 1, 16))
8+
9+
baz: string
10+
>baz : Symbol(baz, Decl(objectRestReadonly.ts, 2, 13))
11+
12+
quux: string
13+
>quux : Symbol(quux, Decl(objectRestReadonly.ts, 3, 13))
14+
}
15+
16+
const obj: Readonly<ObjType> = {
17+
>obj : Symbol(obj, Decl(objectRestReadonly.ts, 7, 5))
18+
>Readonly : Symbol(Readonly, Decl(lib.d.ts, --, --))
19+
>ObjType : Symbol(ObjType, Decl(objectRestReadonly.ts, 0, 0))
20+
21+
foo: 'bar',
22+
>foo : Symbol(foo, Decl(objectRestReadonly.ts, 7, 32))
23+
24+
baz: 'qux',
25+
>baz : Symbol(baz, Decl(objectRestReadonly.ts, 8, 13))
26+
27+
quux: 'quuz',
28+
>quux : Symbol(quux, Decl(objectRestReadonly.ts, 9, 13))
29+
}
30+
31+
const { foo, ...rest } = obj
32+
>foo : Symbol(foo, Decl(objectRestReadonly.ts, 13, 7))
33+
>rest : Symbol(rest, Decl(objectRestReadonly.ts, 13, 12))
34+
>obj : Symbol(obj, Decl(objectRestReadonly.ts, 7, 5))
35+
36+
delete rest.baz
37+
>rest.baz : Symbol(baz, Decl(objectRestReadonly.ts, 2, 13))
38+
>rest : Symbol(rest, Decl(objectRestReadonly.ts, 13, 12))
39+
>baz : Symbol(baz, Decl(objectRestReadonly.ts, 2, 13))
40+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
=== tests/cases/conformance/types/rest/objectRestReadonly.ts ===
2+
// #23734
3+
type ObjType = {
4+
>ObjType : ObjType
5+
6+
foo: string
7+
>foo : string
8+
9+
baz: string
10+
>baz : string
11+
12+
quux: string
13+
>quux : string
14+
}
15+
16+
const obj: Readonly<ObjType> = {
17+
>obj : Readonly<ObjType>
18+
>Readonly : Readonly<T>
19+
>ObjType : ObjType
20+
>{ foo: 'bar', baz: 'qux', quux: 'quuz',} : { foo: string; baz: string; quux: string; }
21+
22+
foo: 'bar',
23+
>foo : string
24+
>'bar' : "bar"
25+
26+
baz: 'qux',
27+
>baz : string
28+
>'qux' : "qux"
29+
30+
quux: 'quuz',
31+
>quux : string
32+
>'quuz' : "quuz"
33+
}
34+
35+
const { foo, ...rest } = obj
36+
>foo : string
37+
>rest : { baz: string; quux: string; }
38+
>obj : Readonly<ObjType>
39+
40+
delete rest.baz
41+
>delete rest.baz : boolean
42+
>rest.baz : string
43+
>rest : { baz: string; quux: string; }
44+
>baz : string
45+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// #23734
2+
type ObjType = {
3+
foo: string
4+
baz: string
5+
quux: string
6+
}
7+
8+
const obj: Readonly<ObjType> = {
9+
foo: 'bar',
10+
baz: 'qux',
11+
quux: 'quuz',
12+
}
13+
14+
const { foo, ...rest } = obj
15+
16+
delete rest.baz

0 commit comments

Comments
 (0)