Categories
TypeScript Best Practices

TypeScript Best Practices —void, Default type Parameters, Spread, and Numbers

Spread the love

No void Expressions

We shouldn’t return expression in function with the void return type.

For instance, if we have the doWork function:

const doWork = (): void => {
  doFirst();
  doSecond();
};

We should call doWork without using its return value.

So instead of writing:

console.log(doWork());

We write:

doWork()

No Conditional Expression

We should use conditional expressions instead of assigning the same thing to each branch of the statement.

For instance, instead of writing:

let foo;
if (cond) [
  foo = 1;
}
else {
  foo = 2;
}

We write:

let foo = cond ? 1: 2;

It’s much shorter and easy to read.

Use Object Spread

Object spread has been a feature since ES2018, so we should use it instead of Object.assign .

For instance, instead of writing:

const baz = Object.assign({}, foo, bar);

We write:

const baz = {...foo, ...bar};

Including the Radix Argument in parseInt

We should include the radix argument when calling parseInt so that it’ll parse the value to the number with the base we want.

It also won’t assume the base of the number we’re parsing based on the value.

For instance, instead of writing:

const x: string = '12';
const dec: number = parseInt(x);

We write:

const x: string = '12';
const dec: number = parseInt(x, 10);

Operands Should be of Type String or Number When Using the Plus Operator

We should make sure that they’re both numbers of strings when we’re using the + operator.

This way, we know that we’re adding or concatenating for sure.

For instance, instead of writing:

const foo = 'foo' + 1;

We write:

const foo = 'foo' + 'bar';

or:

const sum = 1 + 2;

No Usage of this in Static Methods

We shouldn’t use this in static methods.

They shouldn’t reference this since they won’t reference values we expect, which is the class instance.

For instance, instead of writing;

class Foo {
  static foo() {
    return 'foo';
  }

  static bar() {
    return `bar${this.foo()}`;
  }
}

We write:

class Foo {
  foo() {
    return 'foo';
  }

  bar() {
    return `bar${this.foo()}`;
  }
}

Strict Comparisons

We should make sure that we use === and !== for equality comparisons.

They check the type of data and the value.

So instead of writing:

x == 1;

We write:

x === 1;

to avoid any data type coercion before comparison.

Strict String Expressions

We should use interpolation with template literals instead of using string concatenation.

For instance, instead of writing:

'foo' + bar

We write:

`foo ${bar}`

Add Default Clause with Switch Statements

We should add a default clause with switch statements so that we do something when none of the case s match the values.

For instance instead of writing:

let foo: number = 1;
switch (foo) {
  case 1:
    doSomething();
    break;
  case 2:
    doMore();
}

We write:

let foo: number = 1;
switch (foo) {
  case 1:
    doSomething();
    break;
  case 2:
    doMore();
    break;
  default:
    console.log('default');
}

Compare typeof with the Correct String Values

We should compare typeof with the correct string values.

typeof can only return a few string values for the types.

So we should compare against them and make sure we don’t have any typos.

For instance, instead of writing:

typeof foo === 'undefimed';

We write:

typeof foo === 'undefined';

No Unnecessary Constructor

We shoudn’t have constructors that are redundant.

JavaScript will add them for us without it.

For instance, instead of writing:

class A {
  constructor(){}
}

or

class A extends B {
  constructor(){
    super();
  }
}

We write:

class A {
  constructor(name){
    this.name = name;
  }
}

or

class A extends B {
  constructor(bar){
    super(bar);
  }
}

Default Type Parameter

We can add a default type value to the generic type parameter.

For instance, instead of writing:

function foo<N, S>() {}

We can write:

function foo<N = number, S = string>() {}

This way, the generic type parameters will always be set with an argument.

Conclusion

We can add default types to generic type parameters.

void expressions shouldn’t be used as values.

Conditional expressions, object spread, parseInt with a radix are all things we should have in our code.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *