Merge pull request #12434 from Microsoft/Fix12377

Add special handeling for function and array in Object.freeze
This commit is contained in:
Mohamed Hegazy 2016-11-23 13:26:36 -08:00 committed by GitHub
commit 72cee3e045
4 changed files with 75 additions and 0 deletions

12
src/lib/es5.d.ts vendored
View File

@ -176,6 +176,18 @@ interface ObjectConstructor {
*/
seal<T>(o: T): T;
/**
* Prevents the modification of existing property attributes and values, and prevents the addition of new properties.
* @param o Object on which to lock the attributes.
*/
freeze<T>(a: T[]): ReadonlyArray<T>;
/**
* Prevents the modification of existing property attributes and values, and prevents the addition of new properties.
* @param o Object on which to lock the attributes.
*/
freeze<T extends Function>(f: T): T;
/**
* Prevents the modification of existing property attributes and values, and prevents the addition of new properties.
* @param o Object on which to lock the attributes.

View File

@ -0,0 +1,22 @@
tests/cases/compiler/objectFreeze.ts(9,1): error TS2542: Index signature in type 'ReadonlyArray<number>' only permits reading.
tests/cases/compiler/objectFreeze.ts(12,3): error TS2540: Cannot assign to 'b' because it is a constant or a read-only property.
==== tests/cases/compiler/objectFreeze.ts (2 errors) ====
const f = Object.freeze(function foo(a: number, b: string) { return false; });
f(1, "") === false;
class C { constructor(a: number) { } }
const c = Object.freeze(C);
new c(1);
const a = Object.freeze([1, 2, 3]);
a[0] = a[2].toString();
~~~~
!!! error TS2542: Index signature in type 'ReadonlyArray<number>' only permits reading.
const o = Object.freeze({ a: 1, b: "string" });
o.b = o.a.toString();
~
!!! error TS2540: Cannot assign to 'b' because it is a constant or a read-only property.

View File

@ -0,0 +1,29 @@
//// [objectFreeze.ts]
const f = Object.freeze(function foo(a: number, b: string) { return false; });
f(1, "") === false;
class C { constructor(a: number) { } }
const c = Object.freeze(C);
new c(1);
const a = Object.freeze([1, 2, 3]);
a[0] = a[2].toString();
const o = Object.freeze({ a: 1, b: "string" });
o.b = o.a.toString();
//// [objectFreeze.js]
var f = Object.freeze(function foo(a, b) { return false; });
f(1, "") === false;
var C = (function () {
function C(a) {
}
return C;
}());
var c = Object.freeze(C);
new c(1);
var a = Object.freeze([1, 2, 3]);
a[0] = a[2].toString();
var o = Object.freeze({ a: 1, b: "string" });
o.b = o.a.toString();

View File

@ -0,0 +1,12 @@
const f = Object.freeze(function foo(a: number, b: string) { return false; });
f(1, "") === false;
class C { constructor(a: number) { } }
const c = Object.freeze(C);
new c(1);
const a = Object.freeze([1, 2, 3]);
a[0] = a[2].toString();
const o = Object.freeze({ a: 1, b: "string" });
o.b = o.a.toString();