Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Creating a Class in TypeScript


    Typescript classes make traditional object oriented programming easier to read and write. In this lesson we learn about class syntax, what the constructor is and some interesting variable features.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    A typescript class is a function. Functions are objects, so they can have properties. Classes can also have methods. You don't use the word function or a colon when defining a class method, just the name of the method, parens, and curly brackets.

    Notice that I left the opponent argument untyped. For clarity and to catch errors, let's make an opponent type. Now we create some comic book character instances. Let's have Storm attack the Blob. Let's see if this works. Cool, no errors. Let's run the code. Storm attacked the Blob, nice.

    All of the class properties are public by default. We can add the public modifier to any of these properties, but it's unnecessary because that's their default. The secret identity property shouldn't be public, so let's make it a private access modifier. You can't access private class properties outside of the class. Let's see what the compiler says. Secret identity is private and only accessible within the class.

    We can set the private property and have less code by setting all of the properties for the comic book character instances when they are initialized. The class constructor gets called when the class instance is initialized. Now we need to update how the instances set their properties and to make sure that the private property got set, let's add a method to retrieve it.

    Let's call the get secret identity method and see if this works. Nice. Setting the class properties via the constructor is definitely better than before, but Typescript has a shorthand for setting property values when a class instance gets initialized. Now we can get rid of all this code, and we can get rid of all this code.

    Adding access modifiers to the constructor arguments lets the class know that they're properties of a class. If the arguments don't have access modifiers, they'll be treated as an argument for the constructor function and not properties of the class. Let's see if this works. Sweet.

    Typescript classes also have static properties. Static properties are associated with the class, not the instance. Our great team's static method is just returning an object. Notice that if I try to call the static method on the instance, it's unavailable. The static method is only available on the class.

    Let's check this out. Run the code. The console log is returning this object. It's got a name and an array of comic book characters.

    Even though static members can't be called by the instance, they can update instance private members. Let's create a private class property and set that property in our static method. Let's rename the static method to create an assigned team. We already have the object. For each member of the team, let's assign their private team property as the team object that's being created.

    I make sure that this is working. Let's make another method that gets the team name from the instance. We need to clean this part up and call get team name. Run the compiler and the code, nice.

    To review, we've learned about access modifiers and the difference between public and private. The constructor is run when the class instance is initialized, and the shorthand for setting class properties allows us to write less code. We've learned that static properties can only be referenced from the class, not the instance, and how static properties have access to an instances private properties.