mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-09 02:30:15 -06:00
Merge pull request #12434 from Microsoft/Fix12377
Add special handeling for function and array in Object.freeze
This commit is contained in:
commit
72cee3e045
12
src/lib/es5.d.ts
vendored
12
src/lib/es5.d.ts
vendored
@ -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.
|
||||
|
||||
22
tests/baselines/reference/objectFreeze.errors.txt
Normal file
22
tests/baselines/reference/objectFreeze.errors.txt
Normal 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.
|
||||
|
||||
29
tests/baselines/reference/objectFreeze.js
Normal file
29
tests/baselines/reference/objectFreeze.js
Normal 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();
|
||||
12
tests/cases/compiler/objectFreeze.ts
Normal file
12
tests/cases/compiler/objectFreeze.ts
Normal 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();
|
||||
Loading…
x
Reference in New Issue
Block a user