Skip to content

Instantly share code, notes, and snippets.

@KDuverge
Last active December 4, 2019 16:43
Show Gist options
  • Save KDuverge/c501107274a188b2c1c509611649a6e3 to your computer and use it in GitHub Desktop.
Save KDuverge/c501107274a188b2c1c509611649a6e3 to your computer and use it in GitHub Desktop.
Typescript React Coding Standards & Best Practices

Typescript React Coding Standards

This document will show a compilation of resources showing what are proper coding standards and best practices when building a large Typescript React project.

Table of Contents

  1. Best Practices

  2. Resources

Best Practices

Array Annotation

Annotate arrays as string[] as opposed to Array<string>. As it is easier to read and it is used by the Typescript team.

Classes

  • Don't use Classes-related OOP.
  • Stuff like protected, private, public, implements
  • Abstract classes

Generics

  • Avoid using generics, unless you can have a good reason for using them.

Namespaces

  • Don't use them, favor import, and export with file separation.

Interfaces

Types

  • Don't use over interfaces, unless you have a good reason for doing so.
  • Favor Union types over type hierarchies when you need to constrain values.

Constructor

No need to use constructor inside Class components

Don't:

type State = { count: 0 };
type Props = {};

class Counter extends Component<Props, State> {
  constructor(props: Props) {
    super(props);

    this.state = {
      count: 0,
    };
  }
}

Do:

type Props = {};

class Counter extends Component<Props> {
  state = {
    count: 0,
  };
}

Children props

Always Provide explicit type for children props

Don't

// Button.tsx
import React, { Component } from 'react';

class Button extends Component {
  render() {
    const { children } = this.props;

    return <button>{children}</button>;
  }
}

// App.tsx
import React, { Component } from 'react';
import Button from './components/Button';

const App = () => {
  render() {

    return (
      <Button>
        Click me!
      </Button>
    );
  }
}

Do:

// Button.tsx
import React, { Component, ReactNode } from 'react'; // ReactNode is provided by @types/react as type

type Props = {
  children: ReactNode,
}

class Button extends Component<Props> {
  render() {
    const { children } = this.props;

    return <button>{children}</button>;
  }
}

// App.tsx
import React, { Component } from 'react';
import Button from './components/Button';

const App = () => {
  render() {

    return (
      <Button>
        Click me!
      </Button>
    );
  }
}

Enums

  • When you need a constraint set of 2+ possible values, favor Enums heavily.
  • Prefer string enums over implicit enums if the value is relevant.
enum Colors {
  Red = 'Red',
  Green = 'Green',
  Blue = 'Blue'
}

type Props = {
  colors: Colors
}

File Naming

Don't:

SkaterBoy.tsx

userAccessHandlers.ts

Do:

skater-boy.tsx

user-access-handlers.ts

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment