Learn TypeScript

TypeScript is a strict syntactical superset of JavaScript and adds optional static typing to the language.

Companies and DAOs like Streamflow, Astar Foundation, PancakeSwap, Gelato Digital, Blocknative, Aave, Findora Foundation, Pinata, Evmos and Flashbots use TypeScript to work on Streamflow, Astar Network, PancakeSwap, Blocknative, Findora, Pinata, Evmos, Flashbots, Iron Fish and Unstoppable Domains.

Conquer the basics of TypeScript here and go pro once you get the hang of it. Average salaries for TypeScript developers are approximately $75k-125k/yr.

This tour focuses on how TypeScript improves JavaScript, so if you're not familiar with JavaScript - dive into that tutorial first.

TypeScript lets you assign a type to the variable:

let port: number = 8333;

That way you cannot make a mistake later:

let port: number = 8333;
port = "8333"; // error: type 'string' is not assignable to type 'number'

Another cool feature is enums:

enum Port {
  Bitcoin = 8333,
  Ethereum = 30303,
}

const port = Port.Bitcoin;

So TypeScript can catch errors like these:

enum Port {
  Bitcoin = 8333,
  Ethereum = 30303,
}

// error: type '"1234"' is not assignable to type 'Port'
const newPort: Port = "1234";

Functions are the same as in JavaScript, but with types specified:

const fn = (n: number): string => n+'';

If a parameter is optional, add a question mark:

const fn = (n?: number): string => n+'';

fn(3); // "3"
fn();  // "undefined"

Arrays are declared like this:

const add = (numbers: number[]): number => {
  return numbers.reduce((a, b) => a + b, 0);
};

// this works
const sum1 = add([1, 2, 3]);

This doesn't work:

const add = (numbers: number[]): number => {
  return numbers.reduce((a, b) => a + b, 0);
};

// error: type 'string' is not assignable to type 'number'
const sum1 = add([1, 2, '3']);

You can use an "or" statement to accept different types:

type NumberString = number | string;

const join = (numbers: NumberString[]): string => {
  return numbers.map(n => n+'').join(',');
};

const joined = join([1, 2, '3']); // `joined` is now "1,2,3"

TypeScript will even forgive you if you omit the return type:

type NumberString = number | string

const join = (numbers: NumberString[]) => {
  return numbers.map(n => n+'').join(',');
};

const joined = join([1, 2, '3']); // `joined` is now "1,2,3"
Effective TypeScript: 62 Specific Ways to Improve Your TypeScript

If you'd like to go beyond the basics, here's a good read:

Effective TypeScript: 62 Specific Ways to Improve Your TypeScript