8

I'm developing react application using typescript.

Here is my App.tsx. As you can see react has @types but other modules don't

enter image description here

Is there any way to stub these modules or use them as any or another

asked Nov 20, 2017 at 9:20
2

3 Answers 3

18

All the modules you're referring have @types. But let's assume that the module you're using doesn't, e.g. the library kea(keajs) doesn't have @type defined to this date.

The simplest way to solve this is to create a declaration file declarations.d.ts and declare the problematic module like this:

declare module "kea"

Then in your tsconfig.json you need to be sure that in include property you have something like:

...
"include": [
 "src", "declarations.d.ts"
]
...

Or just put declarations.d.ts inside your src folder so you don't need to touch this file.

Be wary that you won’t have any types for the module (the module will be just a big any); but you won’t have any errors.

answered Jun 1, 2020 at 20:56
Sign up to request clarification or add additional context in comments.

Comments

2

If anyone still wondering what to do in case of missing @types/module type definitions, dts-gen can cover the missing type and generate a .d.ts for that specific module.

answered Sep 3, 2021 at 13:46

Comments

-2

Actually, I am pretty sure these modules have @types. Check here:

https://www.npmjs.com/package/@types/react-redux https://www.npmjs.com/package/@types/redux-persist https://www.npmjs.com/package/@types/react-router

If you use NPM, you can install, react-redux for instance, doing:

npm install --save-dev @types/react-redux
answered Nov 20, 2017 at 9:22

7 Comments

So what if i'm using modules that don't have types?
@Herrgott Then you can write a .d.ts file containing some declarations. These declarations can be as easy as any or as complex as you need.
I should write 'any' declarations manually for each package? It looks uncomfortably if i just need a stub for package
It seems to me that i just spent my time with typescript
@Herrgott actually, almost every used package has its own definition. And writing a fake definition should not take more than one line. IMHO, giving up TypeScript just for that is a dramatic error.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.