Skip to content

Conversation

mhegazy
Copy link
Contributor

@mhegazy mhegazy commented Nov 22, 2016

Fixes #12377

* 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;
Copy link
Contributor

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.

Copy link
Member

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.)

Copy link
Contributor

@falsandtru falsandtru Nov 23, 2016

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

Copy link
Member

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).

Copy link
Contributor

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.

Copy link
Member

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

Copy link
Contributor

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.

Copy link
Contributor

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;

@mhegazy mhegazy merged commit 72cee3e into master Nov 23, 2016
@mhegazy mhegazy deleted the Fix12377 branch November 23, 2016 21:26
@mhegazy mhegazy mentioned this pull request Nov 23, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants