GithubHelp home page GithubHelp logo

angular-style-guide's Introduction

Angular / TypeScript projects style guide

TypeScript + ESLint


TypeScript + ESLint

Require that member overloads be consecutive

Use '@typescript-eslint/adjacent-overload-signatures': 'error', because grouping overloaded members together can improve readability of the code.

#f03c15 Bad pattern

class Foo {
    foo(s: string): void;
    foo(n: number): void;
    bar(): void {}
    foo(sn: string | number): void {}
}

export function foo(s: string): void;
export function foo(n: number): void;
export function bar(): void;
export function foo(sn: string | number): void;

#c5f015 Good pattern

class Foo {
    foo(s: string): void;
    foo(n: number): void;
    foo(sn: string | number): void {}
    bar(): void {}
}

export function bar(): void;
export function foo(s: string): void;
export function foo(n: number): void;
export function foo(sn: string | number): void;

Requires using either T[] or Array<T> for arrays

Use '@typescript-eslint/array-type': 'error' for always using T[] or readonly T[] for all array types.

#f03c15 Bad pattern

const x: Array<string> = ['a', 'b'];
const y: ReadonlyArray<string> = ['a', 'b'];

#c5f015 Good pattern

const x: string[] = ['a', 'b'];
const y: readonly string[] = ['a', 'b'];

Disallows awaiting a value that is not a Thenable

Use '@typescript-eslint/await-thenable': 'error'

#f03c15 Bad pattern

await 'value';

const createValue = () => 'value';
await createValue();

#c5f015 Good pattern

await Promise.resolve('value');

const createValue = async () => 'value';
await createValue();

Requires type annotations to exist

Many believe that requiring type annotations unnecessarily can be cumbersome to maintain and generally reduces code readability. TypeScript is often better at inferring types than easily written type annotations would allow. And many people believe instead of enabling typedef, it is generally recommended using the --noImplicitAny and --strictPropertyInitialization compiler options to enforce type annotations only when useful. This rule can enforce type annotations in locations regardless of whether they're required.

But why is annotation description useful? Because for the code review, you will not be able to find out what will change in runtime due to inferred types. But when you specify types, you know exactly what to change to:

It is also worth noting that if we do not use type annotations for methods, it may be difficult for us to read the code during the review, we may not notice something and not pay attention to how the data type has changed if the build does not crash with an error.

Reference to explicit-module-boundary-types

Require explicit return and argument types on exported functions' and classes' public class methods

Explicit types for function return values and arguments makes it clear to any calling code what is the module boundary's input and output.

#f03c15 Bad pattern

// Should indicate that a number is returned
export function myFn() {
    return 1;
}

#c5f015 Good pattern

// A return value of type number
export function myFn(): number {
    return 1;
}

angular-style-guide's People

Contributors

splincode avatar splincode1994 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.