Types You Don't Control 12 exercises
solution

The declare module Syntax in TypeScript

Let's start by creating a new declaration file called my-module.d.ts inside of the src directory.

Inside the file, we'll use the declare module syntax to define a module and indicate what it exports. To match the test from the challenge, we'll export a function named myModuleFunc that retu

Loading solution

Transcript

00:00 Okay, the solution here involves a declaration file. So let's create one. We're going to call it my-module.d.ts And in fact my-module name does not matter.d.ts just for fun and Inside here, we are going to use the declare module syntax. Now declare module here

00:20 I don't just get to say declare module, right? I have to say declare module something and so I'm going to say declare module my-module Now declare module my-module. Inside there, if we look at the index.ts file here

00:34 It's now finding the definition for my-module. So let me just pull this up and let me get this double screened So we've got our d.ts file on the left hand side and we've got our index.ts file on the right Now declare module my-module. We can now say export

00:53 const and we're going to say my-module func and for now, let's just call it a string here Now this is working nicely. We can say we're exporting stuff from this imaginary module. We can't give any Implementation here, of course because we're inside a declaration file So it's going to say const initialize in a new context must be a string numeric literal literal enum reference

01:13 So it's basically saying you can't add implementations into declaration files. So there we go Now my-module func is considered a string. So instead we just need to say it's a function which returns void Beautiful, and now we can basically just get that typed

01:30 So declare module lets you basically say this module with this particular specifier Does something or exports something and of course we can export other stuff from here, too