TypeScript Tutorial #5 - Explicit Types

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

Key Takeaways

Covers explicit types in TypeScript

Full Transcript

okay then going so currently when we write our variables and Asylum values typescript automatically infers the variable type based on the value we give it but sometimes we might just want to initialize a variable without giving it a value so then it can't really infer its type and then we could give it any value in the future but we can get around this by explicitly giving the variable a type so how do we do that well say for example I wanted to declare a variable called character and I wanted this to be a string but I don't know what the value of that string is going to be yet well I could say that this is gonna be a string by doing colon and string like so and now we're saying okay initialize this variable but don't give it a value but in the future only allow us to give this a value of a string and I could do the same for numbers or billions so for example I could say let's age be a number by saying let age colon number and same for boolean let is logged in and then that is going to be a boolean okay so now we're giving these three things types and if we hover over one of these things we can see it's a string this is a number and this is a boolean so now say for example I wanted to give age a value well then if I tried to give it a value of a string like Luigi it's not gonna let me do this and we get an error and that's because we've already explicitly given this age variable a type so we can't do this but I could say age is equal to some kind of number like 30 and that is fine same for is logged in I couldn't set that equal to something like a number because we already said this should be a boolean but I could say oops is is logged in not is locked like that but I could change it to true or false so I could say is logged in is now equal to true and that's absolutely fine okay so there the basic types that's how we can explicitly define what types are going to be inside a variable but what about arrays they're a little bit more complicated right so say for example have an array I'll say let ninjas and this is going to be an array now if I want this to be an array of strings I could say : and then string square brackets and now in the future this can only be an array of strings so if I say ninjas is now equal to 10 and 23 that's not going to be allowed because these are not strings but they could then be something like Yoshi and Mario that is allowed now sometimes you want to initialize this to be an empty array to begin with because then we can use the push method on this if we didn't have that here and we didn't initialize it with a value we couldn't say dot push and then add on a string because at the minute it's not actually an empty array and we're not getting an error here but if I save this now and go to the browser we get an error here it says cannot read property push of undefined and that's because we've not actually initialized this with a value yet of an empty array we're saying in the future it should be a string array and then we're trying to push something onto it but we've not yet declared that string array I hope that makes sense so sometimes it's best to initialize it with an empty array and then we can only play strings inside that array so now if I save this it should work we don't get an error okay okay so what if I want to create some kind of mixed array so I could have strings and numbers in it and maybe billions as well well for that we could use something that is known as a union type and a union type is basically our way of saying it could be one of two or one of three types so say I have an array called mixed and I want this to be an array we put our type before that in a second I'll set it equal to an empty array to begin with but right here instead of it just being a string array I also want it to be numbers and billions that we can store it here as well well how do we do that we'll use a union type and to do that we can open up parentheses and say in here it could be a string or and we use a pipe to say or number and now we could store strings and numbers inside this thing right here so I could come down here and say mixed push and it's going to be a string hello that would be fine no error I could say mixed push and this will be 20 and that would be fine but if I then say mixed push and it's going to be a boolean so I could say false then we should get an error because we've not said this can be a boolean but if I now add another pipe on and say boolean right here this is saying that this array can be any of these three types right here so these are known as Union types when we declare something like this and it can be one of several different types it's known as a union type so let's just log these to the console to see what it looks like mixed and we've pushed those three variables to it save it and we can see right here hello 20 unfolds so that all works awesome now we can also use Union types on normal variables not just a raise so say for example I create a new variable like a UID and that could be either in string format or number format well I could say this is going to be a string or a number now when we're using a union type that is not in front of an array we don't have to use parentheses you do if it's in front of an array but you don't if we're just giving a union type to a normal variable like this okay so now UID could be and there a string so I could say 1 2 3 that's fine or UID could be a number 1 2 3 but UID couldn't be something else like a boolean and that's not going to work okay all right then so finally objects how do we explicitly declare a variable as an object well we could just simply say let ninja 1 we'll call this variable be an object type and now we could say ninja 1 is equal to any kind of object so I could say an object where there's a name property and that's going to be Yoshi and then also an age property and that's going to be 30 so that's absolutely fine because this is still an object and saying that ninja want must be an object but we then couldn't say ninja one is equal to a string for example that wouldn't work all right and by the way we could still declare this to be some kind of an array that would work because an array is actually a kind of object so that is allowed but if you want to be more specific when declaring the type of object you can be so I could say down here for example let ninja to be an object and we can do it explicitly like this so it's not equal to an object here we're just declaring the type it should be in the future and we're saying right here it should be a type of object where it has a name property and that should be a string yeah and then we have an age property which should be a number and maybe also a belt color property which should be a string so now we're saying right here okay ninja 2 is an object but it must have these three properties inside it name age and belt and the types of those should be string number and string so now if I try to say ninja 2 is equal to an object at the minute because we don't have these properties then we're not allowed to do that it has to have all of these properties with these values so if I add on name to be Mario and then also the age right here to be 20 and then finally the belt color is going to be black this now works we don't get any errors at all but if I try to add on the fourth property skills which is an array now we get an error and it won't let us do this okay so this is how we explicitly type our variables before we give them a value

Original Description

🐱‍👤🐱‍👤 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

Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Prepare for a frontend developer interview with Capgemini by reviewing JavaScript fundamentals and practicing common interview questions
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with 10 essential tools for modern web app development
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with top 10 developer tools in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
US frontend engineer hiring demand peaked in 2022 and remains flat-depressed in 2026, contrary to common assumptions
Dev.to AI
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →