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