What a class is
You already work with built-in types such as number, string and arrays. A class lets you create a type of your own. It describes what a kind of thing holds and what it can do, and it works as a blueprint: the class is the plan, and the objects you build from it are the actual things.
Suppose a program deals with dogs. Each dog has a name and an age, and it can bark. Without a class you would keep a separate variable for every name and every age, and write functions that take those variables as loose arguments. A class bundles the data and the behaviour under one name, Dog, so the rest of the program can talk about dogs directly.
In TypeScript, a class does two jobs. At run time it is a real JavaScript class that builds objects with new. At compile time its name is also a type, so you can write let d: Dog and the compiler checks that you only put dogs there.
The syntax
The smallest class is a name and a pair of braces:
class ClassName {
}
classis the keyword that starts a class definition.ClassNameis the name you choose. By convention class names use PascalCase: every word starts with a capital letter and there are no underscores, as inDog,BankAccountorHighScore. The compiler accepts other styles, but the convention makes classes easy to tell from variables at a glance.- The braces hold the class body. Fields, a constructor and methods will go there in the next lessons. An empty body is valid, and the class still exists.
There is no semicolon after the closing brace. Unlike a variable declaration, a class definition is a declaration that ends with its block.
The exercise
The lesson gives you two lines that already use Dog:
let dog = new Dog();
console.log("Defined the Dog class!");
The first line creates an object from the class. The second prints a message. Without the class, the first line has nothing to build from, and the compiler reports that it cannot find the name Dog.
- Write the class above the existing lines. Use the
classkeyword followed byDog, the exact name used below. - Leave the braces empty. Nothing else is needed yet.
The finished program:
class Dog {
}
let dog = new Dog();
console.log("Defined the Dog class!");
Running it prints:
Defined the Dog class!
Details and common mistakes
- Capitalisation matters. Names are case-sensitive. A class called
dogwould not satisfynew Dog(), becauseDoganddogare two different names. - The order matters. A class declaration is not usable before the line where it appears. If
new Dog()ran before theclass Dogline, the program would fail with aReferenceErrorat run time, and the compiler would also complain that the class is used before its declaration. Put the class first. - The type exists only while compiling. After compilation to JavaScript, annotations like
: Dogare gone. The class itself stays, since it is real JavaScript. - An empty class accepts almost anything. TypeScript compares object types by their shape, not by name. A class with no members has an empty shape, so many values fit it. That changes as soon as you add fields and methods, which is what the next lessons do.
- Defining is not creating. The class by itself produces no dog. Only
new Dog()does.
Seeing the class at run time
A class is a value as well as a type. In the following snippet, typeof Dog reports function, because a JavaScript class is a special kind of function, and instanceof tells you whether an object was built from a class:
class Dog {
}
let dog = new Dog();
console.log(dog);
console.log(dog instanceof Dog, typeof Dog);
Dog {}
true function
The first line shows an object of class Dog with nothing inside it yet, which is what an empty class gives you.
Key takeaways
- A class is a blueprint that groups data and behaviour under one name.
- Define it with
class Name { }, using PascalCase for the name. - An empty body is valid. The following lessons fill it in.
- Declare the class before the code that creates objects from it.
- In TypeScript the class name is both a run-time value and a type.