dnmh
dnmh

Reputation: 2135

Importing a typescript module to a JS module in a React app

I have a React app created with create-react-app. Recently I've added some source code for a module I need. It's in Typescript and will stay in Typescript. Not my choice. I've installed what I needed to build Typescript and that part seems to work OK.

However, when I try importing that TS module to my regular jsx, it says module not found.

To illustrate:

shared/
    - MyTSModule/
        - index.tsx
    - OtherModule/
        - index.jsx
view/
    - MyView/
        - index.jsx

I'm trying to import MyTSModule to MyView. The error is: Module not found: Error: Can't resolve 'shared/MyTSModule' in...

I'm importing like all the other modules from shared:

import MyTSModule from 'shared/MyTSModule ';

What am I missing?

Upvotes: 2

Views: 2990

Answers (1)

elderapo
elderapo

Reputation: 374

You cannot import typescript file from javascript. You either need to:

  1. compile typescript to javascript and then import compiled file
  2. convert your javascript files to typescript.

I personally recommend porting all files to typescript, this way you will have more type safety.

Upvotes: 3

Related Questions