TypeScript Tutorial #16 - Interfaces with Classes

Net Ninja · Beginner ·🌐 Frontend Engineering ·6y ago

Key Takeaways

Demonstrates how to use interfaces in conjunction with classes in TypeScript

Full Transcript

right there my friends so we've seen how to use interfaces to define the basic structure of objects in the last lesson now I want to show you how we can use interfaces alongside classes as well so what I'm going to do is create a new folder inside source called interfaces this is where all of our interface files are going to live and I'm going to create a new one called has formatter es so inside here I'm going to create a new interface I'm also going to export it so we can use it in other files as well so say export first then we want to export an interface which is called has formatter now inside here we can define the different properties or methods that anything which implements or uses this interface should have now I don't want to add any properties I just want to add a method and that method is going to be a format method and this format method should return a string okay so that's it that's the only thing that I want this interface to ensure that whatever uses it has this format method so I can use this with a class now to say that a class must have this format method if it uses this interface right so if I go to our invoice class over here I can say that I want to implement the has formatter interface now first of all I need to import it so let me say import double curly braces from we need to come out of the classes folder then into the interfaces folder and then it has formatter dot Jas okay and then we want to import has formatter from that now if we want to implement the has formatter interface inside a class then we can say implements right here and then whatever interface that we want to implement inside this class so has formatter and now we're saying look this class must follow the structure of not that the has formatter interface so it must have this formats method inside it which returns a string now in our case it does so this passes there's no error it successfully implements that okay so it's following this structure and we're just ensuring that we have to follow this structure so that in the future we can be sure that every object that we create using this class follows this has formatter interface so if we create an array for example in the future and we only want objects to be added to that which implements this has format interface we can be sure that anything that goes in there using this class has that has formatter interface I hope that makes sense it will become a bit clearer later on when we start to use this elsewhere but anyway we've now implemented that if we didn't have this then we get an error okay so we have to have that former method all right so let's try creating another class which also implements this has formatter interface so I'm going to copy all this right here and I'm going to create a new file called payment dot CS and I'm gonna paste that or right in here and this class is going to be called payment and then the clients is no longer going to be a clients but a recipient instead and down here we want to output recipients instead of clients so it's pretty much the same but instead of a client property we now have a recipient property because when we make a payment it's going to someone when we raise an invoice we're getting money from someone and we'll change the text as well so we'll say is owed because we owe it to them so it would say something like Yoshi is old X amount for whatever he did okay so we have those two classes and they both implements the has formatter interface so in the future for example we could create some variables let me first import a couple of things up here we want invoice we also want payment and that comes from the payment JavaScript file and we also want the has formatter so import and it's going to be has formatter and that's gonna be from dot forward slash interfaces forward slash has formatter jeaious okay so we're importing those three things now imagine we create a couple of variables so I'll say let doc one be of type has formatter so we're saying here in the future this variable must be of type has formatter it must implement that interface and I'll do the same for another variable doc - and now what I could do is create a new instance of the invoice and store it in dock one because we know that the invoice implements this has formatter interface and then for dock - we could set that equal to a new payment using this class because we also know that that payment class implements has a formatter so that's absolutely fine they're both of that type if that makes sense so I'm going to sing now dock 1 is equal to new invoice and the first parameter is going to be Yoshi that's the client for the invoice the second parameter is just going to be the details web work and the third want the amount which is 250 and then on to that I'll make a new payment so I'll say doc 2 is equal to a new payment and that also implements the house formatter interface this time it's going to be - Mario and that's going to be for plumbing work and the amount is going to be 200 so that's fine doc 1 can be an invoice doctor can be a payment because they both implement this interface so this is just making sure that whatever object in the future is stored inside this doc 1 variable and this doctor variable has to implement this interface it just dropped our code a little bit more and makes things a little bit more secure for ourselves in the future now another thing if we wanted to create an array which only holds objects which implements this has format interface we can do so I could say let Docs be some kind of has formatter array and initialize it to be a new array now we're saying the only objects which implement this interface can go inside that array we can say down here docs dot push and it's going to be doc 1 and that's fine because that implements the has formatter interface and Doc's push doc 2 which is also fine because that implements the has formatter interface okay so we're just restricting what can go in here now and it can come from several different classes this object as long as they both implements that has formatter interface and that way we can be sure that every object inside here has a format function which returns a string so when we're cycling through this we can be guaranteed that we can use the format function on every string now so that's the benefit let me just log these to the console so I'm going to say console dot log the docs and save it let me just see this works and we can see we have an invoice and the payment inside here awesome alright so now we know how to do all that why don't we put it to good use in our project down here and let me get rid of these as well we don't need those invoices down here inside this form when we add an event listener and that's submit we're getting all of the values from these different things right here at the type 2 in front details and the amounts now what I want to do is check the type first of all is it going to be either an invoice or a payments and then create a new invoice or payments object so what I'm going to do first is down here say let doc be some kind of variable which implements has format so in the future this can only be an object which implements that interface then under that I'm going to do an if check so I'll say if and then the type which is this thing right here remember the type is what they choose right here if the type value is going to be equal to invoice at that point we're going to say that we want to create a new invoice object and store it inside dock so we'll say that dock is equal to a new invoice right here and the first argument is going to be the two from so if we got to invoice we can see that the first one is the client and that's from this two from field right here so we'll say two from value the second one is going to be the details so details that value and the third one is the amount so amount and then we say value as number like so because it expects a number to be the type okay so that's the new invoice right there otherwise we're going to create a new payment object instead source a dock is equal to new payment and it's the same things go in so I'm going to grab this and paste it right here because we need a string which is two from then the details then the amount because the same things go in here we have a recipient which is a string the details and then the amount so now down here instead of logging out all of these things I can console dot log the dock right so let me save that and come over here and when we submit something will say payment to Yoshi and that's going to be for doing some marketing the amount is gonna be a hundred pounds at it and we can see we have a payment object right here the recipient is Yoshi the detail is doing some marketing the amount is 100 if I create an invoice to Mario for doing some website work and that is going to be for 200 and add that we now have an invoice object awesome so now we have these objects available to us when a user is submitting this form in the next video what we'll start to do is create a which can take these objects and render them to the screen

Original Description

ey gang, in tis TypeScript tutorial we'll take a look at how we can use interfaces in conjunction with classes. 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join ---------------------------------------- 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Course Files: https://github.com/iamshaunjp/typescript-tutorial 🐱‍💻 🐱‍💻 Other Related Free Courses: + HTML & CSS Crash Course - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G + Modern JavaScript - https://www.youtube.com/playlist?list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc 🐱‍💻 🐱‍💻 TypeScript Docs: https://www.typescriptlang.org/docs/home.html 🐱‍💻 🐱‍💻 The Net Ninja Community Boards: https://community.thenetninja.co.uk/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →