TypeScript Tutorial #8 - Function Basics

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

Key Takeaways

Covers TypeScript function basics

Full Transcript

all right the gang so in this video I'd like to talk about the function type in typescript so first of all let's create a function and store it inside a variable which I'm going to call greet and this is going to be an error function and all we'll do is log to the console hello world okay so a simple function now type script will automatically infer that the type of this variable now is going to be a function much like in third strings or billions or other types before when we defined those and now in the future if I try to say that greet was equal to another type like a string for example then it's going to give me an error and it's going to say that I can't do that because we already have declared that this is going to be of type function okay so we can't do that now instead of defining a value for a specific variable which will be a function we don't have to do that we can explicitly say that this will be a function in the future much like we did with other types by saying greet : and then function with a capital F and notice this time the function the type is with a capital unlike string unlike boolean or anything else where we didn't use capitals so function it does use a capital now we still can't do this because we're saying that greet should be a function book what we can do is later on set greet equal to some kind of function so I can say greet is equal to an error function and inside here I'm just gonna say console dot log and we'll say hello again and this is allowed it lets us do this because we've said greet is a function okay so we've already seen as well how we can pass in parameters or arguments into a function and say what types those should be but let's just go through that again I'm going to come down here and create a constant and call it art and set that equal to a new function and I'm going to pass in a few parameters into this function so a which will be of type number we've seen this before this is how we specify the type of the parameters which must be passed in and B is also going to be a number and then down here we'll just console dot log a plus B so if I try to call this function I'd say add and then passing two numbers like five and ten okay so if I save it we should see that in the console right 15 which is 5 plus 10 but I can't pass through 10 hours say a string because it must be a number so what if I want to pass in maybe an optional parameter because at the minute if I don't pass in one of the parameters I get an error maybe I want one of the parameters to be optional maybe a third parameter well I can do that I can say C and then I'm going to say that this must be a number or a string remember this is a union type so we're saying this parameter must be either a number or a string now if I want this to be optional all I have to do is place a question mark in front of it and notice when I do that the error down here goes away so put a question mark now it's optional and now it goes away even though I don't pass it through as a third argument so when we don't pass it through the value of this becomes undefined and I can demo that by saying console dot log and output C and if I save it we should see undefined over here okay so that's an optional parameter likewise a bit like optional parameters we can also say that this will have a default value if we don't pass a number through so I could say that this default value is going to be equal to 10 okay now at this point when we use a default value we don't need this optional parameter because we're saying okay well it's still going to be optional but the default parameter is going to be 10 if they don't pass something in so we don't tend to use the question mark and the default parameter together just one or the other so now instead of being undefined if we don't pass through a third argument for C it will now refer to this default value which is 10 and if I save and check this out in a browser we see 10 is logged down here but if I pass in a third parameter 20 then I see that one instead you know rides a default value and this could be a string 20 if you wanted it to be because we said that this is a union type number or string and that works as well okay so that's the basics of parameters inside functions and by the way if you use an optional parameter or default parameter you should always generally put these at the end of the parameters so always do your required parameters first because they have to be passed in and the optional parameters at the end otherwise these arguments are going to get mixed up all right okay so when we create a function we sometimes also return a value right so let me create another function I'm going to say Const - is equal to a function where we take in two parameters a is a number and also B is going to be a number as well and inside here we're just going to return a plus B that's all we're doing now if I then come down here and say let results equal to - and then passing two numbers like ten and seven what will happen is that this right here will become the type of the return value right here so when a function returns something like this typescript can infer the type that is returned and therefore if we hover over this we can see that results is a number because it's inferred the return type of this function it's giving it a value and it knows that this will be a number so now if we try to say that results later on was equal to something else like a string we can't do that because we can't change the type it automatically inferred the return type and set that type to this where we set it equal to that function okay now we can also if you want to explicitly declare the type or return type rather of a function up here and we do that after the function parentheses right here by adding a colon and then the return type number now we don't need to do that because typescript is autumn Atika inferring the return type so we don't need to do it but I have seen it in some cases and I sometimes use it as well explicitly just to show to other developers if we have a bigger function that look this function is going to return a number right so we can see that at a glance now what if we don't return something like this function or right here because this just looked something through the console well it still technically returns a value and it returns a value called void and if we hover over this we can see this void keyword if you look to the right of this thing that's popped up on the screen over here it says it returns void at the end so that would be like would seem explicitly right here void so a function in typescript returns this void value when we don't actually return something and it basically represents a complete lack of return value now when this is compiled into JavaScript that will become undefined but in typescript void is completely separate to undefined undefined would be like before when we had an optional parameter over here and if we didn't pass something through it would become undefined write void is a complete lack of any kind of value now that probably sounds a bit foggy but don't get bogged down too much with that now we'll probably see more examples of using void and undefined in future lessons but now hopefully you have a better understanding of how function types work in typescript

Original Description

Hey gang, in this TypeScript tutorial we'll talk more about Functions, optional arguments & return types. 🐱‍👤🐱‍👤 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
How to Open KIT Files (CodeKit Project)
File Extension Geeks
Watch →