Skip to content

Class expressions #3568

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

Merged
merged 8 commits into from
Jun 24, 2015
Merged

Class expressions #3568

merged 8 commits into from
Jun 24, 2015

Conversation

ahejlsberg
Copy link
Member

This PR adds type checking for class expressions, finishing the work started in #2567.

In a class expression, the class name is optional and, if specified, is only in scope in the class expression itself. This is similar to the optional name of a function expression. It is not possible to refer to the class instance type of a class expression outside the class expression, but the type can of course be matched structurally.

Class declarations and class expressions differ as follows:

  • A class declaration of the form class C { } introduces an instance type named C and a constructor function named C with the type new () => C.
  • A class expression of the form class C { } produces a constructor function value of type new () => C and introduces an instance type named C that is in scope only within the body of the class expression.
  • A class expression of the form class { } produces a constructor function value of type new () => { ... } where { ... } is the anonymous instance type of the class expression.

An example:

let Point = class {
    constructor(public x: number, public y: number) { }
    public length() {
        return Math.sqrt(this.x * this.x + this.y * this.y);
    }
};
var p = new Point(3, 4);  // p has anonymous class type
console.log(p.length());

Fixes #497.

if (node) {
return node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.ClassExpression;
}
return node && (node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.ClassExpression);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this !!node so this never returns undefined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to, undefined is just as good.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 23, 2015

👍

ahejlsberg added a commit that referenced this pull request Jun 24, 2015
@ahejlsberg ahejlsberg merged commit c556749 into master Jun 24, 2015
@ahejlsberg ahejlsberg deleted the classExpressions branch June 24, 2015 00:21
@xLama
Copy link

xLama commented Sep 7, 2015

Should typeof operator works with class expressions?

var B = class {}
function foo(a:typeof B):void{}
foo( new B() );  // Error

@ahejlsberg
Copy link
Member Author

@xLama It works, but typeof B is the type of the constructor function, not the instance type that the constructor function returns. So:

var B = class {}
function foo(a:typeof B):void{}
foo(B);  // Ok

@xLama
Copy link

xLama commented Sep 7, 2015

@ahejlsberg
So

var B = class {}
var b = new B();
function foo(a:typeof b):void{}
foo( new B() );  // Ok

@ahejlsberg
Copy link
Member Author

@xLama Exactly.

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

7 participants