2

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?

1
  • Are you only processing through typescript with js enabled? Commented Feb 17, 2020 at 10:14

1 Answer 1

3

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.

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

1 Comment

I thought compiling TS to JS would happen on the fly and that compiled JS would automatically be imported.

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.