TypeScript Tutorial #14 - Modules
Key Takeaways
Explains how to split code into multiple files using the ES6 module system in TypeScript
Full Transcript
alright then gang so currently we have been writing all of our code in this one single file right here app TS and in the past in sandbox TS as well now that is absolutely okay for learning but when we're making a larger project typically it's easier to split our code into different modular files for example we might have one file which manages all the Dom interactions and maybe another file which manages database interactions or authentication or something else now this would make the project more structured and much easier to read and update in the future as well now the best way we can do that in typescript is by using the es6 module system where we can import and export things from different files when we need to now modern browsers support es6 modules already in vanilla JavaScript so we can just use them in typescript as well now it's important to note though that only modern browsers support this feature out-of-the-box typescript doesn't compile the module system down into something older browsers understand as well so I would recommend using a modern browser such as Chrome or Firefox to work with this if you are using webpack in the future with your typescript project then maybe you could sidestep that issue by bundling your code into one single file and in the future I will do a mini web pack with typescript series which explains all of that but for now let's try this module system in typescript so the first thing we need to do before we start working with this module system is to go into the TS config file find the module property right here and we're gonna change it to es 2015 so that is the regular Atmos group 2015 specification of the module system that's what we're going to use the regular JavaScript version I'm also going to change this to es6 for the output because we're just targeting modern browsers to do this so I'm gonna save that right there we also need to go to index.html and find where we have a reference to this script and we need to say that it's going to be a module so we do that by saying type is equal to module like so and now we're all set up and ready to use the module system so we do this say for example I want a separate file to have this class in it so it's not with the rest of the code down here well first of all let's go into source and create a new folder and I'm going to call this classes you could call it something else if you want like models or something I'm going to call it classes and then create a new file inside that and that is going to be called invoice dot TS so now I'm going to grab all of this class right here from the very top down to here not all of this stuff below it where we instantiate the class because we're still going to do that from app dot es just the top bit where we define the class itself and then go to invoice and then save it okay so we have this right here now this class defined but over here now we can't access that class because it's no longer inside this file and if I try to run this we get an error over here saying invoice is not defined so what we need to do if we want to use the invoice inside this file is import that invoice from this file but first of all we have to export it from this file now that's simple enough all we need to do is put export in front of this and now we're exporting this invoice class now if we want to import that class to another file all we have to do is go to the top right here and then I'm gonna say import curly braces from and we'll come back to this curly braces in a minute and we need to say what file we want to import it from now we want to go into the classes and then the invoice file so it's dot forward slash classes to go into the classes folder then invoice and you would think it would be dot es but it's not it's dot J's because at the end of the day we're compiling this down into JavaScript and the browser is going to import the JavaScript file not a typed grit file okay so that's important even when we're working with typescript we use the dot J's extension when we're importing something okay so what do we want to import from this file well we say that inside the curly braces right here now we export invoice right here so we just need to say invoice in here and we need to spell it correctly oh I see won't work like so so if I save that now everything should still work the same and we can see we can't still see all this stuff right here so that's all there is to it first of all we update this to use this module system and we also say inside index over here that we're using a module and then all we need to do is export whatever we want to export from a certain file and then import that into a new file like this when we want to use it dead simple so we've managed to use a module system here which greatly helps with our code organization and it will make things easier to maintain in the future as well but there are two major drawbacks here first of all the browser support only modern browsers support this module system so if a user is using an outdated browser or an older browser then you might get an error and it might not work and secondly it doesn't bundle our code into a single file the browser is still using the module system to load separate files and make multiple requests when we compile these things down notice in the public folder it didn't compile it down into a single file it compiled it down into both of these two files and it even created this class's folder for us if we look inside these we can see that we're still importing invoice and inside invoice we're still exporting this class right so we still have two javascript files and we're making separate requests for those finals if I go to the network tab we can see that one request for a PS and one request for invoice yes so that is the second downfall it's making these multiple requests now to combat both of these downfalls you click throw webpack into the mix that is going to bundle our code into a single file when it's completed and that way we only have one network request and all browsers are supported to now I will do a mini-series on getting started with typescript and web pack in the future but for now let's move on and talk about something else in typescript interfaces
Original Description
Hey gang, in this TypeScript tutorial we'll see how we can split our code into multiple files using the ES6 module system.
🐱👤🐱👤 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