Skip to content

Commit 72cee3e

Browse files
authored
Merge pull request #12434 from Microsoft/Fix12377
Add special handeling for function and array in Object.freeze
2 parents c2bef6d + 15d870b commit 72cee3e

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

src/lib/es5.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ interface ObjectConstructor {
176176
*/
177177
seal<T>(o: T): T;
178178

179+
/**
180+
* Prevents the modification of existing property attributes and values, and prevents the addition of new properties.
181+
* @param o Object on which to lock the attributes.
182+
*/
183+
freeze<T>(a: T[]): ReadonlyArray<T>;
184+
185+
/**
186+
* Prevents the modification of existing property attributes and values, and prevents the addition of new properties.
187+
* @param o Object on which to lock the attributes.
188+
*/
189+
freeze<T extends Function>(f: T): T;
190+
179191
/**
180192
* Prevents the modification of existing property attributes and values, and prevents the addition of new properties.
181193
* @param o Object on which to lock the attributes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
tests/cases/compiler/objectFreeze.ts(9,1): error TS2542: Index signature in type 'ReadonlyArray<number>' only permits reading.
2+
tests/cases/compiler/objectFreeze.ts(12,3): error TS2540: Cannot assign to 'b' because it is a constant or a read-only property.
3+
4+
5+
==== tests/cases/compiler/objectFreeze.ts (2 errors) ====
6+
const f = Object.freeze(function foo(a: number, b: string) { return false; });
7+
f(1, "") === false;
8+
9+
class C { constructor(a: number) { } }
10+
const c = Object.freeze(C);
11+
new c(1);
12+
13+
const a = Object.freeze([1, 2, 3]);
14+
a[0] = a[2].toString();
15+
~~~~
16+
!!! error TS2542: Index signature in type 'ReadonlyArray<number>' only permits reading.
17+
18+
const o = Object.freeze({ a: 1, b: "string" });
19+
o.b = o.a.toString();
20+
~
21+
!!! error TS2540: Cannot assign to 'b' because it is a constant or a read-only property.
22+
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [objectFreeze.ts]
2+
const f = Object.freeze(function foo(a: number, b: string) { return false; });
3+
f(1, "") === false;
4+
5+
class C { constructor(a: number) { } }
6+
const c = Object.freeze(C);
7+
new c(1);
8+
9+
const a = Object.freeze([1, 2, 3]);
10+
a[0] = a[2].toString();
11+
12+
const o = Object.freeze({ a: 1, b: "string" });
13+
o.b = o.a.toString();
14+
15+
16+
//// [objectFreeze.js]
17+
var f = Object.freeze(function foo(a, b) { return false; });
18+
f(1, "") === false;
19+
var C = (function () {
20+
function C(a) {
21+
}
22+
return C;
23+
}());
24+
var c = Object.freeze(C);
25+
new c(1);
26+
var a = Object.freeze([1, 2, 3]);
27+
a[0] = a[2].toString();
28+
var o = Object.freeze({ a: 1, b: "string" });
29+
o.b = o.a.toString();

tests/cases/compiler/objectFreeze.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const f = Object.freeze(function foo(a: number, b: string) { return false; });
2+
f(1, "") === false;
3+
4+
class C { constructor(a: number) { } }
5+
const c = Object.freeze(C);
6+
new c(1);
7+
8+
const a = Object.freeze([1, 2, 3]);
9+
a[0] = a[2].toString();
10+
11+
const o = Object.freeze({ a: 1, b: "string" });
12+
o.b = o.a.toString();

0 commit comments

Comments
 (0)