TypeScript Tutorial #11 - The DOM & Type Casting

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

Key Takeaways

Works with the DOM and uses type casting in a TypeScript tutorial

Full Transcript

alright then going so just like in regular JavaScript we can use typescript to interact with the Dom to query elements of data HTML react to events like button clicks or farm submits etc now before we dive into that I just want to explain one thing I've deleted the sandbox ES file right here and I've replaced that with an app dot CS file because we're going to start to create the code now for this up and I've also had that compiled even though it's empty at the minute - up Jes down here and inside the index file I've linked to that app.js file so that's all I've done so far so then for the most parts when we work with the dubbing typescript it is the same as when we work with the Dom in JavaScript we can still use the same query methods and event listeners etc and we can still access all of the same properties on the Dom elements but there are a few key differences to be aware of so first of all let's just dive right into an example I'm going to say Const anchor is equal to document dot query selector just like we would in JavaScript and that is going to grab an anchor tag okay so that's exactly the same so far as we would do in JavaScript and inside our HTML we have an anchor tag or right at the bottom in the footer right here so it should grab that that's the only one on the page now then watch this if I come down here and say console dot log and I want to log the anchor tag out and save this then over here we can see that we have the anchor tag but if I try now to access a property of it such as href which we can do we get some kind of error now why is that well if we hover over this it says that this object is possibly null so it's saying well this right here might actually be null and the reason it's saying that is because typescript doesn't actually know whether there is an anchor tag in the index page during developments it doesn't have some kind of special access to the index page to be able to find that out so it's just warning goes here that look this could be null and if this is norm then you're not going to get an H ref property on it so we can combat this in one of two different ways first we could do some kind of a runtime check we could say if anchor and if that is true it means it's not null then we can log this out to the console and that's going to take away the error and then this will work right or the other way if I logged out let me just paste that down here again currently we get the error if we are certain as developers that this thing exists inside our HTML file what we can do is add on an exclamation mark to the end of that query selector and now that error goes and that is also as developer sane look I know that this is going to return some kind of value an anchor tag and not null I know that for definite so you don't need to give me this error down here so now it knows that this is actually going to be an HTML element and we can access this property on it now another cool thing about using typescript for Dom interaction is that it automatically contains special types for every Dom elements so if I hover over this we can see that Const anchor is an HTML anchor element that is the special type of this variable this means that when we use the anchor variable typescript knows all of the different properties and methods on that type and it's going to make them available to us in some kind of intelligence so if I say anchor dots it knows it's an anchor tag and so we can use all of these different methods or properties on that that are available to an anchor tag which is nice okay so let's move on to another example I'm going to now try and grab this form right here so it's a form tag but it also has a class of new item form so let me just comment out all that jazz first of all and then what I'm gonna do is say Const form is equal to documents dots query select all and we want the form tag now again I could say exclamation mark to say look I know this exists and when I do that and hover over it I can see that this now as a type of HTML form element now this is fine but what if I had several different forms on one page well we couldn't be certain that this gets the right form on the page there so we could use a class instead so if I comment this out what I could do instead is say Const form is instead equal to documents dots query selector and I think it's called the new item form we want this class right here let me copy that dude and paste it right here like so and it's gonna still grab that same thing but even if I add an exclamation mark over here when I hover over this now it's gonna say that this is of type element and not the HTML form elements so why is that well it's because when we use form or anchor tag typescript knows what tag we're grabbing now in this case we're just passing a class and a class could be applied to any different element in the HTML page so it doesn't know that it's a form it just knows that it's going to be some kind of elements so how do we combat this well we can use something known as typecasting to say what this is going to be what type of element this is going to be we can cast it to be a certain type and the way we do that is by saying as and by the way we take off our exclamation mark when we use us because if we say as it's never going to be null it's always going to be of a certain type that we cast it to so I could say as and then it's gonna be a tml form elements okay so now when it grabs this instead of storing it as element type it uses this type instead HTML form elements and if I hover over that now I can see that so therefore we get all of the right properties and methods available to us in some form of intellisense inside the vs code which is nice okay so now I could say something like console.log and I'm going to get the form I could say dots to get all of the different properties and methods available to us and I could choose one of these if I go to the top and just gonna choose something like children if I can find it there it is okay cool save it oops there we go and we should see all of those right here an HTML collection with these different fields awesome that's all of the children of the form okay so let's try that and grabbing the different input fields inside that form so I could say inputs and then down here I'm going to say Const type and I'm saying Const ight because it's this field I'm grabbing first of all and I don't just want the fields I want the actual inputs themselves so I'm gonna say that constant type is equal to documents dot query selector and then I can't say input because I don't know which import I'm going to get them instead I want to go over here and use the different IDs that I've given to these different form fields so this first one right here has an ID of type so we'll say hash type to grab the ID now again if I hover over this it just says it's gonna be elements or null it says or null because we've not done the exclamation mark now it's just elements but I can typecast this and I could say as HTML select elements okay because this is a select field right here awesome so now I want to get the second one and that is going to be in fact what we'll do is just duplicate this and this time it's not going to be type it's going to be two from this is going to be this thing right here this input so I'm going to grab the ID two from and paste it right here this time it's not a select element it's an HTML input elements okay and that's this thing right here so we can do the same for the other two so I'm going to go down again and this time it's going to be details so let me copy that and paste it over here and over here as well and that is also an input element and the third one if I go down is going to be called a mount and the ID of that you'll just have to trust me is a mount and that is also an input field okay so this is still inputs elements awesome so now we have access to all of those input fields why don't we now try adding an event listener to the form so that when a user submits the form we can access these different fields and maybe lock them to the console so let's do that I'm going to comment out this log up here first of all then down here I'm going to grab the form which we have stored right here and I'm gonna say add events listener it's going to be a submit event and then I'm going to fire a callback function when this occurs this is going to take in a parameter E which is of type events built into typescript it's an event object and then inside this function I want to first of all say e dot prevents default that prevents the page refresh in the default behavior when we submit a form and then down here I'm going to console dot log all the different values so first of all type dot that's this thing right here and dot value just gets us the value that is currently inside the Select or the input field so type top value first of all then to from dot value then we want details doc value and then finally we want amounts don't value as well okay so let's see if this works come over here so we'll change this to payments we'll say yoshi and we'll say website work and then the amount is going to be 200 add let's see this works we see payment yoshi website work and 200 now I deal it in the future I want this to be a number but by default even though this is a number right here that were inputting JavaScript turns that into a string version of the number now what we could do to combat this is say instead of just a value value as number and then it turns this into a number forest so if I do this again just put any old junk in here and then do an amount of 400 AD this is now a number and we can see that because it's blue in the console and when we say blue number in the console it means it's of number type and not a string cool so there we go my friends that is the crux of interacting with the Dom in typescript we'll build on this later but for now I wanna move on to something else classes

Original Description

Hey gang, in this TypeScript tutorial we'll how how to work with the DOM. 🐱‍👤🐱‍👤 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 →