give it a try
and you'll save time
Sorry for terrible rhyme :(
a.k.a. strong opinions
bundle exec rails webpacker:install:typescript// tsconfig.json
{
"compilerOptions": {
"module": "system",
"noImplicitAny": true,
"removeComments": true,
"outFile": "./lib/bundle.js",
"sourceMap": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
// webpack.config.js
module.exports = {
mode: "development",
devtool: "inline-source-map",
entry: "./app.ts",
output: {
filename: "bundle.js"
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: [".ts", ".tsx", ".js"]
},
module: {
rules: [
// all files with a `.ts` or `.tsx`
// extension will be handled by `ts-loader`
{ test: /\.tsx?$/, loader: "ts-loader" }
]
}
};Magic words: `any` and `void`
function distance(source, destination) {
return Math.sqrt(
Math.pow(source.x - destination.x, 2) +
Math.pow(source.y - destination.y, 2)
)
}
// is actually seen by typescript as:
function distance(source: any, destination: any): any {
return Math.sqrt(
Math.pow(source.x - destination.x, 2) +
Math.pow(source.y - destination.y, 2)
)
}function distance(source: any, destination: any): any {
return Math.sqrt(
Math.pow(source.x - destination.x, 2) +
Math.pow(source.y - destination.y, 2)
)
}
// with types:
function distance(
source: { x: number, y: number },
destination: { x: number, y: number }
): number {
return Math.sqrt(
Math.pow(source.x - destination.x, 2) +
Math.pow(source.y - destination.y, 2)
)
}function distance(
source: { x: number, y: number },
destination: { x: number, y: number }
): number {
return Math.sqrt(
Math.pow(source.x - destination.x, 2) +
Math.pow(source.y - destination.y, 2)
)
}
// DRY it:
interface Vector { x: number, y: number }
function distance(source: Vector, destination: Vector): number {
return Math.sqrt(
Math.pow(source.x - destination.x, 2) +
Math.pow(source.y - destination.y, 2)
)
}interface Vector { x: number, y: number }
function distance(source: Vector, destination: Vector) : number {
return Math.sqrt(
Math.pow(source.x - destination.x, 2) +
Math.pow(source.y - destination.y, 2)
)
}class User {
name: string
age?: number
role = 'guest'
constructor(name: string, age?: number, role?: string) {
this.name = name
this.age = age
if (role) this.role = role
}
isAdmin() {
return this.role === 'admin'
}
}import React, { FunctionComponent } from 'react';
type Props = {
title: string,
paragraph: string
}
// we can use children even though we haven't defined them in our CardProps
const Card: FunctionComponent<Props> = (props) => (
<div>
<h2>{ props.title }</h2>
<p>
{ props.paragraph }
</p>
{ props.children }
</div>
)🍰 Sweet IDE integration 🍰
Expose dangerous code 🐛
Text
twitter: @MaciejWalusiak
GitHub: github.com/Rabsztok
https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html
https://github.com/typescript-eslint/typescript-eslint
https://babeljs.io/docs/en/babel-preset-typescript
http://jonathancreamer.com/why-would-you-not-use-typescript
https://fettblog.eu/typescript-react
https://blog.logrocket.com/7-bad-excuses-for-not-using-typescript-dbf5e603a9a8/