This course has been updated! We now recommend you take the TypeScript Fundamentals, v3 course.

Check out a free preview of the full TypeScript Fundamentals course:
The "Methods" Lesson is part of the full, TypeScript Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Mike shows that methods can be defined in a manner similar to objects. Static methods can be defined using the static keyword.

Get Unlimited Access Now

Transcript from the "Methods" Lesson

>> Mike North: So we can define methods in the similar way that we do on objects. Note that there are no commas in between the different methods, you don't have commas when you're defining a class as you do in your defining an object literal. And there is recently, recently was added a static keyword if you're using like the latest babble transforms and things, this should be available to you.

[00:00:22] Where you can create essentially a function that is attached right to the constructor, right to the class itself, and does not require any instance to be created. So note that we were creating a stranger, and for some reason here, we've got a random number that is gonna be generated.

[00:00:43] Let's see, person.createRandom, yep, so the person's name is a random number. We pass that in, and what we've done here is we've created, think of it like an alternate constructor, right. Just like we can, our first instance has to be created by something that's available on the class in order to give us this first instance.

[00:01:04] We can create static functions to act as sort of alternate constructors to use in various scenarios, particularly when we have some type safety around these, it becomes even more useful, right?