Vue 3 with TypeScript Tutorial #2 - Vue Components using TypeScript

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

Key Takeaways

Builds a Vue 3 application using Vue Components and TypeScript

Full Transcript

alright and gang so as we saw in the last video view components look a little bit different when we're working with typescript first of all this script has a lang attribute of typescript or ts telling view we're using typescript not javascript then the way we're actually creating the component right here this object is different too we're using something called define component to make it so whenever we create a new component using typescript we generally make it this way the rest of the component the way we make the template and also the styles as well that is exactly the same so let's try working with typescript inside our component now to begin with in this lesson i'll be using the options api but in every lesson thereafter i'm going to be using the composition api so let's start by adding a bit of data so let's create this data function which returns an object where we can place our data now i'm just going to create a name property which will be a string now when we're using basic built-in types like strings numbers booleans etc these types will be inferred by typescript automatically so we don't need to specify the type explicitly typescript is going to see this and infer its type as a string and that means that this data property must always be a string from this point on so let's try using this in the template let's go up to the template and we're going to create instead of this a paragraph tag where we're going to output the name so if i save this i'm going to preview this in a browser we can see a link over here so everything is working all right then so what if we want to change this value at some point well let's create a method to do this so below data i'm going to create the methods property and inside here we can create a function which is going to change the name so i'm going to call this change name like so all right so this is going to take in an argument and this argument is going to be the new name that we want to change it to now notice straight away we get some kind of warning or error right here if we hover over it it says name is declared but its value is never read and also parameter name implicitly has an any type so that is typescript saying look you're not specifying the type that this should be well we always want it to be a string right so we'll say string right here and now the error goes away so what we're doing is typing the argument so now only a string can be passed in as the name we don't want a number to be passed in because we can't update this to be a number so inside here now i'm just going to say this dot name and that means grab the property name for this component and i want to set it equal to the name we take in this argument right here so that's all there is to it pretty simple now what if we specified this to be a number right here well this is fine but now notice we get an error right here and if we hover over that it says type number is not assignable to type string so this is what i was talking about a minute ago typescript inferred this type to be a string and we cannot then change it into a number in the future typescript doesn't allow us to do that so it must always be a string and because this function knows that this is going to be a number it's giving us this error right here to say look we can't do this we can't update the name with whatever number is coming in here so this must be string all right cool so let's try adding this in the template as well we'll do a button so that when a user clicks on the button it changes the name and we're going to pass in an argument so button first of all and we'll say change name inside that button and we'll attach a click event to the button and that is going to be equal to some kind of function that function is change name so we're going to invoke that function we also want to pass in an argument so parentheses now if we try to pass in a new name for example zelda like that it's absolutely fine because this is a string the function accepts a string if we try to pass in a number then we're going to get an error again and if we hover over this it says argument type number is not assignable to parameter of type string so it's saying look we're accepting this parameter which is a string and you're trying to pass in a number so this has to be a string so this is just one of the ways that using typescript is benefiting us in this instance it's telling us before we even preview our code in the browser that something is wrong so let's change this back to zelda and see if this works i'm going to go to the browser change name and yes it works all right then so let's try another example i'm going to add in another property called age and this is going to be say 25. now in this instance i want the age to be a number however i also want to allow the age property to be a string in the future if we change it as well not just a number now if i left it like this typescript would infer this to be just a number and never allow us to change it to a string so we want to explicitly type this property to be a number or string how do we do that well normally in typescript when we type something we would say okay here's a variable i'm going to call it age and then i would do a colon and then say the type so if i wanted this to be a number then i'd type it to be a number and then set the value for example 25. if i wanted it to be a string then this would be string now you can see we get an error over here because this is not a string so it would have to be a string so 25 in quotes for example now if i wanted this to be a string or a number then i would use a union type which is just two types next to each other with a pipe symbol in between so string or number essentially so now if this is a string it's fine and now if this is a number it's fine but inside the data object right here we can't use this kind of syntax to type it because this is a data property and not a variable like this so how do we do it inside the data object right here well we can use something called type assertion otherwise known as type casting and this is a way that we can explicitly tell view that a property will be of a certain type so we could define an initial value like this and then say as and number pipe string so a union type right here meaning it can be either a number or a string so in the future now we're saying this can be a number as it is or we can change it to a string so this is how we type things inside the data object right here we use type assertion using the as keyword okay all right then so let's output that in the template up here i'm going to do a dash and then output the age as well and save it oops we need curly braces there not square brackets save that and now we can see 25 change name still works i also want to create a function to change the age as well so let's do that down here so change age and inside here we're going to take in the age argument but remember this could be two different types now it could be a string or a number now you could have it as just number to update it that would be absolutely fine and it's not going to give you any kind of errors because this can be a number we don't have to specify here the argument must be a number or a string as long as it is one of these it doesn't matter so inside here i could say this dot age is equal to the age parameter right here and there's no errors likewise i could change this to string and there's no errors this all works fine i'm going to pass in the union type to say string and a number so it can be either one of these and again there's no error however if i added on something like a boolean as well then there is going to be an error and if we hover over this it says type string number boolean is not assignable to type string number so we have to take this off all right then so that is the function let's try using it i'm going to duplicate this button and i'm going to change this to age and now inside here i'm going to change this to 30 and we get an error that's because the function needs updating as well this should be change h all right then so this obviously works there's no error there if we save it and try it change age it works now if i change this to a string 35 for example and save this then it's still going to work because this can be a string or a number if i change this to something else like a boolean then we get an error right here all right then cool so let's go back and change this to 30. now one more thing to do with these functions before we move on i'm just going to return the name value right here and also return age just so i can show you that typescript automatically infers the return value of these functions as well so if we hover over this you can see to the right of the function over here it says string and that is typescript telling us look this is always going to return a string this function if we hover over this one we can see to the right of the function if we hover over this string or number so it knows this is always going to be a string or number all right then so now we've seen how to use typescript a little bit inside view components using the options api next up i want to move on to using it with the composition api

Original Description

Hey gang, in this tutorial I'll show you the basics of working with Vue 3 components (with the options API) using TypeScript. 🐱‍👤 View this course in full without ads on Net Ninja Pro: https://netninja.dev/p/vue-3-with-typescript-jump-start 🐱‍💻 Course Files: + https://github.com/iamshaunjp/vue-with-typescript 🐱‍👤 JOIN THE YOUTUBE NET NINJA GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS 3 & Firebase - https://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 Useful playlists: + Vue 3 Tutorial - https://www.youtube.com/watch?v=YrxBCBibVo0&list=PL4cUxeGkcC9hYYGbV60Vq3IXYNfDk8At1 + TypeScript Tutorial - https://www.youtube.com/watch?v=2pZmKW9-I_k&list=PL4cUxeGkcC9gUgr39Q_yD6v-bSyMwKPUI 🐱‍💻 🐱‍💻 Other links: + Get VS Code - https://code.visualstudio.com/ 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
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 Reads

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