Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React + TypeScript

React + TypeScript

This talk give some insights into using ReactJS and TypeScript.

Agenda
- TypeScript: Why we need it and how we can add it to our projects
- React + TypeScript: How to add TypeScript to a React project
- React + Redux: How TypeScript helps to write the Redux part of our application
- Conclusion: What is good / where are problems

Daniel Mies

October 19, 2017
Tweet

More Decks by Daniel Mies

Other Decks in Programming

Transcript

  1. 7 JavaScript is great... Runs everywhere High flexibility Many libraries

    Dynamic typing Errors at runtime Design errors (this, prototype) Bad IDE support Dynamic typing but not perfect
  2. errors at compile time non-invasive easy learnable 8 Solution: TypeScript

    clean JS output •Developed by Microsoft •open source •since October 2012 •stable since January 2014 •conforms to EcmaScript standards and proposals
  3. TypeScript 9 TypeScript EcmaScript 1 superset of EcmaScript 2 optionally

    typed (static &structural) 3 compiles to ES3, ES5, ES201*
  4. TypeScript 9 TypeScript EcmaScript 1 superset of EcmaScript 2 optionally

    typed (static &structural) 3 compiles to ES3, ES5, ES201* 4 no special runtime
  5. 11 Basic project setup npm init -y npm install --save-dev

    typescript tsc --init this generates a tsconfig.json where the TypeScript configuration is stored.
  6. Interfaces 13 interface User { username: string; password: string; }

    const user: User = { username: 'Bob', password: 'secret' }
  7. Interfaces & structural typing 14 interface Named { name: string;

    } class Person { name: string; } const p: Named = new Person();
  8. Interfaces & structural typing 14 interface Named { name: string;

    } class Person { name: string; } const p: Named = new Person(); p is of type Person and Named
  9. Classes 15 class Greeting{ constructor(private greeting:string = 'World'){ } greet

    = () => console.log(`Hello ${this.greeting}`); } const greeter = new Greeting(); greeter.greet();
  10. Literal Types 16 type Digit = 0 | 1 |

    2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; let nums: Digit[] = [1, 2, 4, 8]; // Error! '16' isn't a 'Digit'! nums.push(16);
  11. Setup 19 1 update your tsconfig 2 add a TypeScript

    loader to your webpack config (awesome-typescript-loader)
  12. Setup 19 1 update your tsconfig 2 add a TypeScript

    loader to your webpack config (awesome-typescript-loader) 3 rename your files: *.js becomes *.ts and *.jsx becomes *.tsx
  13. Setup 19 1 update your tsconfig 2 add a TypeScript

    loader to your webpack config (awesome-typescript-loader) 3 rename your files: *.js becomes *.ts and *.jsx becomes *.tsx 4 add types for react and other libraries
  14. webpack.config.js 21 module.exports = { entry: "./src/index.ts", output: {filename: "./dist/app.js"},

    resolve: {extensions: [".js", ".ts", ".tsx"]}, module: { rules: [{ test: /\.tsx?$/, loader: "awesome-typescript-loader", }] }, };
  15. StatelessComponents 23 import * as React from "react"; import {

    StatelessComponent } from "react"; type TextProps = { text: string; }; export const Text: StatelessComponent<TextProps> = ({ text }) => (<p>{{ text }}</p>);
  16. Components 24 type CounterProps = { start: number }; type

    CounterState = { count: number }; export class Counter extends Component<CounterProps, CounterState>{ constructor(props){ super(props); this.state = {count: props.start}; } render(){ return (<p onClick={() => {this.setState({count: this.state.count+1})}}>{this.state.count}</p>) } }
  17. StatelessComponents (any) 25 import * as React from "react"; import

    { StatelessComponent } from "react"; export const Text: StatelessComponent<any> = ({ text }) => (<p>{{ text }}</p>);
  18. Actions 27 export const ADD_TODO = "ADD_TODO"; export type AddTodoAction

    = { type: typeof ADD_TODO; text: string; }; export const addTodo = (text: string): AddTodoAction => ({ type: ADD_TODO, text });
  19. Reducer 28 export const reducer = (state: AppState = [],

    action: ActionTypes) => { switch (action.type) { case ADD_TODO: { return [ ...state, {completed: false, text: action.text} ] } //... type ActionTypes = AddTodoAction | EditTodoAction | ...;
  20. Reducer 28 export const reducer = (state: AppState = [],

    action: ActionTypes) => { switch (action.type) { case ADD_TODO: { return [ ...state, {completed: false, text: action.text} ] } //... type ActionTypes = AddTodoAction | EditTodoAction | ...; action has type AddTodoAction here
  21. Reducer 28 export const reducer = (state: AppState = [],

    action: ActionTypes) => { switch (action.type) { case ADD_TODO: { return [ ...state, {completed: false, text: action.text} ] } //... type ActionTypes = AddTodoAction | EditTodoAction | ...; action has type AddTodoAction here export type AddTodoAction = { type: typeof ADD_TODO; text: string; };
  22. 31 TypeScript & React Feedback at compile time First class

    IDE support Better API (types) Easy refactoring up to 2x code missing types for some libs PropTypes + TypeScript somehow redundant No first class support