Create-react-app: Loader for TypeScript dependencie

Created on 30 Oct 2018  路  3Comments  路  Source: facebook/create-react-app

Is this a bug report?

Maybe.

I'm starting a project with your very newly create react app --typescript :)

I import a class located in .tsx file, from the project, in App.tsx :

import Home from './components/home/Home';

Classic scenario, works fine.

I'm trying to import a class located in .tsx file, from an NPM module, in App.tsx, :

import Home from "@npmmodule/home/Home";

I've got this issue when I run my npm start :

./node_modules/@npmmodule/home/Home.tsx 4:0
Module parse failed: The keyword 'interface' is reserved (4:0)
You may need an appropriate loader to handle this file type.

Do I need to implement a loader myself to handle that ?

NB : The files are exactly the same. This is the code of both Home.tsx :

import * as React from 'react';
import './Home.scss';

interface IHomeProps {
  value: string,
  name: string
}

interface IHomeState {
  merge: string
}

class Home extends React.Component<IHomeProps, IHomeState>{
  constructor(props: IHomeProps) {
    super(props);
    this.state = {
      merge: props.value + props.name
    };
  }

  public render() {
    return (
      <div>Working. Merge: { this.state.merge }</div>
    )
  }
}

export default Home;

Most helpful comment

Back when I tried to do something like this (with lerna) it was necessary to compile the Typescript component, since the Typescript compiler will expect everything in node_modules to be compiled already.

All 3 comments

I'm not sure is that related, but It's also not possible to import interface from external dependency.

import { withNamespaces, WithNamespaces } from 'react-i18next';

Attempted import error: 'WithNamespaces' is not exported from 'react-i18next'.

Interface WithNamepsaces is exported from /node_modules/react-i18next/index.d.ts

export interface WithNamespaces extends WithI18n {
  tReady: boolean;
  initialI18nStore?: {};
  initialLanguage?: string;
}

Back when I tried to do something like this (with lerna) it was necessary to compile the Typescript component, since the Typescript compiler will expect everything in node_modules to be compiled already.

You cannot consume uncompiled TypeScript from npm. Be sure to compile the package before publishing it!

If this package is not yours, file an issue asking the package maintainer for a compiled copy.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

akctba picture akctba  路  80Comments

razvan-soare picture razvan-soare  路  161Comments

amuehl picture amuehl  路  79Comments

gaearon picture gaearon  路  85Comments

godmar picture godmar  路  130Comments