-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Comments
@btc You can remove the angular example and modules from package.json except typescript and it should work? |
Here is an example setup - https://www.typescriptlang.org/docs/handbook/react-&-webpack.html |
For others who follow in my steps, here are the steps I took to get Typescript working with a Rails project generated using
{
"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
}
module.exports = {
test: /.(ts|tsx)$/,
loader: 'ts-loader'
}
// 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')),
)
})
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 |
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. |
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 const { environment } = require('@rails/webpacker')
environment.loaders.set('typescript', {
test: /.(ts|tsx)$/,
loader: 'ts-loader'
});
module.exports = environment |
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 |
Is there an easy way to get started with Typescript without Angular?
The text was updated successfully, but these errors were encountered: