Search code examples
reactjstsx

Importing jsx in tsx


I have inherited a bit of a legacy project with JSX. I am in the process of converting the JSX into TSX.

Currently while importing JSX and JS files in TSX. I get an error Cannot find module 'config/abc_module' or its corresponding type declarations..

I understand the types are missing as it is a JS file. I have added JSDoc types, but nothing seems to help. What would be the best way to add types in JSX files to use them in TSX.

I have quite a few files in JS, and some I cannot even convert due to specific reasons. I have searched a lot and read quite a few articles, but can't seem to find any specific way forward. Any help would be appreciated. Thanks in advance.


Solution

    1. make a global.d.ts file in your root directory where all your code files are
    2. in file write lines:

    declare module 'path/to/your/js/or/jsx'

    now these js/jsx files will resolve as modules to import in your typescript code! enjoy!

    Edit: also make sure your tsconfig.json is configured to let you mix ts/js