Using Declaration Files with JavaScript in TypeScript
Let's shift gears to look at the practical use case of declaration files.
Consider this example.js
JavaScript file that exports myFunc
:
// example.jsexport const myFunc = () => { return "Hello World!";};
The myFunc
function is then imported inside of a TypeScript `ind
Transcript
00:00 We've been talking about declaration files so far, but we've not really covered their actual main use case. So inside here, we have an index.ts file in which we're importing myfunc from example, and we are then calling myfunc here. But we're getting a big old error from here saying,
00:18 could not find a declaration file for this module that we're trying to import from because it's a JS file. And this sometimes happens, you'll sometimes have JS files in your TypeScript application. And notice that the error is saying, could not find a declaration file for it.
00:37 So your job is to try to use a declaration file to essentially type this or describe the exports of this example.js file. This is really what declaration files are for. And so you're gonna find, I think some pretty easy docs links to get you there. Good luck.