7

Is there a way to export a JavaScript React Class Component into a TypeScript file?

I have the following code:

Login.js

class Login extends Component {

constructor(props) {
    super(props);
    this.usernameText = React.createRef();
    this.passwordText = React.createRef();
    this.urlText = React.createRef();
    this.keyPress = this.keyPress.bind(this);
  }

  .
  .   
  .
 }
 export default  Login;

index.ts

const Login = require("./uiManagement/jiraUI/Login/Login");
export {
 Login as JiraUiLogin 
}

In another project i did an npm import for the above to use the login component:

import { Component } from "react";
import { JiraUiLogin } from "infosysta-typescript-core";
class mLogin extends Component {
  render() {
    return (
      <JiraUiLogin />
    )
  }
};
export default mLogin

But I'm having the following error: enter image description here

5
  • what is the value of moduleResolution property in your tsconfig.json file? Commented Jan 30, 2023 at 9:31
  • in the core project is: "moduleResolution": "node", Commented Jan 30, 2023 at 10:12
  • There might be wrong path in index.ts then. Please check the path first Commented Jan 30, 2023 at 10:20
  • 1
    const Login = require("./uiManagement/jiraUI/Login/Login.js"); try adding the extension, if that doesn't work use import instead of require Commented Jan 31, 2023 at 18:28
  • Any chance you can share your tsconfig.json with us? Commented Feb 3, 2023 at 10:51

2 Answers 2

1

What about changing the Login.js file to typescript?

import React, { Component } from "react";

interface Props {
}
interface State {

}

class Login extends Component<Props, State> {
  usernameText = React.createRef<HTMLInputElement>();
  passwordText = React.createRef<HTMLInputElement>();
  urlText = React.createRef<HTMLInputElement>();
  keyPress = (event: React.KeyboardEvent) => {
    
  };
}

export default Login;
Sign up to request clarification or add additional context in comments.

1 Comment

that's what i did eventually and i used rollup to export my library and it worked
0

I think that you need to add "allowJs": true in the compilerOptions of your tsconfig file, and use an import statement instead of require.

Comments

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.