diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index 7ee478178021d..1e6b44788b37b 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -176,6 +176,18 @@ interface ObjectConstructor { */ seal(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(a: T[]): ReadonlyArray; + + /** + * 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(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. diff --git a/tests/baselines/reference/objectFreeze.errors.txt b/tests/baselines/reference/objectFreeze.errors.txt new file mode 100644 index 0000000000000..9c28c62f5346b --- /dev/null +++ b/tests/baselines/reference/objectFreeze.errors.txt @@ -0,0 +1,22 @@ +tests/cases/compiler/objectFreeze.ts(9,1): error TS2542: Index signature in type 'ReadonlyArray' 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' 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. + \ No newline at end of file diff --git a/tests/baselines/reference/objectFreeze.js b/tests/baselines/reference/objectFreeze.js new file mode 100644 index 0000000000000..4c37631bb1057 --- /dev/null +++ b/tests/baselines/reference/objectFreeze.js @@ -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(); diff --git a/tests/cases/compiler/objectFreeze.ts b/tests/cases/compiler/objectFreeze.ts new file mode 100644 index 0000000000000..5e8539831b1fa --- /dev/null +++ b/tests/cases/compiler/objectFreeze.ts @@ -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();