Skip to content

Conversation

@mhegazy
Copy link
Contributor

Fixes#12377

*/
freeze<T>(a: T[]): ReadonlyArray<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

@falsandtrufalsandtruNov 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.

interfaceObjectConstructor{freeze<TextendsFunction>(f: T): T;}newObject.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 listnew(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;

@mhegazymhegazy merged commit 72cee3e into masterNov 23, 2016
@mhegazymhegazy deleted the Fix12377 branch November 23, 2016 21:26
@mhegazymhegazy mentioned this pull request Nov 23, 2016
@microsoftmicrosoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for freeto 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.

6 participants

@mhegazy@falsandtru@ahejlsberg@RyanCavanaugh@msftclas