If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
Here we have a JavaScript function that determines if a particular string is appellant room or not. Then we use this function with two examples. First madam, which is appellant room because it reads the same forwards and backwards, and then another example that is not appellant room. It's great that we can use this function within this file, but what's even better is if you could use this general purpose utility in other files in our code base. The solution to this task is provided by JavaScript modules, which are also supported first class per type script as an example.
00:34
Let's go ahead and export this function from this file using the export JavaScript keyword that makes this function importable by other files within our code base. As an example, let's jump to another file in the same folder. Now, the first step is to import the module into this file. The general syntax for importing modules is import, followed by the kind of import, followed by the keyword from, followed by the path to the module. In our case, the module is located in the same folder in a file details, so we can use the relative path slash details.
01:11
Now, TypeScript understands everything that is exported from this module and will provide you with nice auto complete and error checking in case of any mistakes. For our use case, we simply import the function is Berlin and Rome, and now we can cut the usages of the function from util Ts and then paste them into index Ts. And because of that import, they will now work as if this function was locally defined within this file. As a final note, all of the syntax of JavaScript ES modules is supported by TypeScript. For example, we can bring in the module
01:45
as a namespace import into a namespace called util, and then for usages, we can access the function is Belen Rome from this namespace.