-
Notifications
You must be signed in to change notification settings - Fork 13k
Add special handeling for function and array in Object.freeze #12434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
* 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>(f: (...args: any[]) => T): (...args: any[]) => T; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
freeze<T extends (...args: any[]) => any>(f: T): T;
would be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, freeze<T extends Function>(f: T): T;
might be even better. (@mhegazy I was mistaken about constraints not being factored into overload resolution.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However, even this definition doesn't work with classes and generic functions. Readonly interface doesn't work well because of some limitations of the current type system. I hope TypeScript will resolve these limitations.
related #9366
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@falsandtru I think the definition I suggested works for both classes and generic functions (but it doesn't solve the problem in #9366).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ahejlsberg It doesn't seem to work.
interface ObjectConstructor {
freeze<T extends Function>(f: T): T;
}
new Object.freeze(class { }); // index.ts(4,1): error TS2350: Only a void function can be called with the 'new' keyword.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@falsandtru You need extra parentheses:
new (Object.freeze(class {})); // Implicit empty parameter list
new (Object.freeze(class {}))(); // Explicit parameters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, then there is no problem with this pr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mhegazy Can you use freeze<T extends Function>(f: T): T;
instead of the following your definition?
+ freeze<T extends (...args: any[]) => any>(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.
+ */
+ freeze<T extends new (...args: any[]) => any>(c: 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>(f: (...args: any[]) => T): (...args: any[]) => T;
Fixes #12377