Skip to content

Typescript Integration (with React) #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
btc opened this issue May 12, 2017 · 7 comments
Closed

Typescript Integration (with React) #371

btc opened this issue May 12, 2017 · 7 comments

Comments

@btc
Copy link
Contributor

btc commented May 12, 2017

Is there an easy way to get started with Typescript without Angular?

@gauravtiwari
Copy link
Member

@btc You can remove the angular example and modules from package.json except typescript and it should work?

@gauravtiwari
Copy link
Member

@btc
Copy link
Contributor Author

btc commented May 13, 2017

For others who follow in my steps, here are the steps I took to get Typescript working with a Rails project generated using --webpacker=react:

  1. Add new dependencies to package.json by executing yarn add ts-loader typescript @types/react @types/react-dom.
  2. Run bin/yarn to update yarn.lock
  3. Copy tsconfig.json from a Rails project generated using --webpacker=angular or from the code block below:
{
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],
    "module": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5"
  },
  "exclude": [
    "**/*.spec.ts",
    "node_modules",
    "public"
  ],
  "compileOnSave": false
}
  1. Add option "jsx": "react" to the compilerOptions section of tsconfig.json.
  2. Create config/webpack/loaders/typescript.js with the following contents:
module.exports = {
    test: /.(ts|tsx)$/,
    loader: 'ts-loader'
}
  1. Rename your generated hello_react.js to hello_react.tsx and make it valid typescript:
// Run this example by adding <%= javascript_pack_tag 'hello_react' %> to the head of your layout file,
// like app/views/layouts/application.html.erb. All it does is render <div>Hello React</div> at the bottom
// of the page.

import * as React from 'react';
import * as ReactDOM from 'react-dom';

interface HelloProps { name: string; }

const Hello = (props: HelloProps) => (
  <div>Hello {props.name}!</div>
)

document.addEventListener('DOMContentLoaded', () => {
  ReactDOM.render(
    <Hello name="React" />,
    document.body.appendChild(document.createElement('div')),
  )
})
  1. Since we have a type checker, we can remove the prop-types package by executing yarn remove prop-types.
  2. Add .tsx to the list of extensions in config/webpacker/paths.yml.

et Voila.

For those who would like a shortcut, I published a Rails 5.1 React Typescript starter project here at https://github.com/btc/rails-starter-2017

@btc
Copy link
Contributor Author

btc commented May 13, 2017

Given that Typescript and React work so well together, how would maintainers feel about making Typescript-flavored React available as a generator option? e.g. --webpack=react-ts

@btc btc changed the title Using Typescript without Angular (instead with React) Using Typescript with React May 13, 2017
@btc btc changed the title Using Typescript with React React Typescript generation May 13, 2017
@btc btc changed the title React Typescript generation Typescript Integration (with React) May 13, 2017
@gauravtiwari
Copy link
Member

@btc Not sure if we should add this to webpacker itself since this is very project specific requirement. Perhaps, another gem (webpacker-react-ts) could setup an installer or may best to document this in README/guide #372

@KurtPreston
Copy link

For anyone having problems with these instructions after upgarding to webpacker 3.2, I found that I was able to get it working by deleting the config/webpack/loaders/typescript.js file, and instead declaring the loader in config/webpack/environment.js:

const { environment } = require('@rails/webpacker')

environment.loaders.set('typescript', {
  test: /.(ts|tsx)$/,
  loader: 'ts-loader'
});

module.exports = environment

@boblail
Copy link

boblail commented Jan 11, 2021

For anyone in finds this thread in 2021, the instructions above don't work on 5.2, but there are docs for how to set this up: https://github.com/rails/webpacker/blob/v5.2.1/docs/typescript.md#upgrading-to-51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants