0

I pulled down a library that contains Typescript and it's javascript version.

When declaring import Library from "@scope/library", my app can only access the typescript, even when I add the js extension.

How can I import the js file instead? I get errors on types.

2 Answers 2

1

You can override the package's default entry point:

const myObj = require("../node_modules/library/path/to/dist/file.js");

like you would reference any other file in your project structure.

Another way to get Typescript to shut up about typings is to require instead of import:

const myObj = require("@scope/library");

This way you don't have to worry about resolving exactly the right js file, and can still benefit from the Node module resolution mechanism.

Sign up to request clarification or add additional context in comments.

4 Comments

I considered this but feared 'bad practice'.
Yeah it is a bit of a backdoor solution, but if it gets you unblocked maybe that's fine :) Caution: the project folder structure and the file names and routes could be different in another version of the package. So, be on the lookout for that if you update your dependencies. Finally, if this is a library you care about, consider contributing to its source and fixing whatever's wrong with their types so that you and others in the community can benefit from the improvement.
I appreciate the additional information, it is exactly what I was looking for. Keeping up with the new and the old javascript methods has been a rough spot in the learning process. Thanks!
Oh yes! I had a similar experience. Had done some front-end coding years ago (think jQuery era) then took a break for a while. Coming back to the world of Javascript coding was just astounding! But hang in there, there's a lot of good resources and smart people out there to help, and Javascript has really become a vibrant viable language, community, ecosystem :)
0

If you're getting issues with the types, you either need to install the types for the package

npm install -D @types/<package> yarn add -D @types/<package>

Or you can ignore types by requiring instead of importing:

const package = require('package')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.