Skip to content

Instantly share code, notes, and snippets.

@cedrickchee
Last active January 13, 2019 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cedrickchee/d3c965d9569a302b167891263183025a to your computer and use it in GitHub Desktop.
Save cedrickchee/d3c965d9569a302b167891263183025a to your computer and use it in GitHub Desktop.
Breakdown of TypeScript type notation: http://2ality.com/2018/04/type-notation-typescript.html
interface Array<T> {
  concat(...items: Array<T[] | T>): T[];
  reduce<H>(
    callback: (state: H, element: H, index: number, array: T[]) => H,
    firstState?: H): H;
  ···
}

This is an interface for an Array whose elements are of type T that we have to fill in whenever we use this interface:

  • method .concat() has zero or more parameters (defined via the rest operator ...). Each of those parameters has the type T[]|T. That is, it is either an Array of T values or a single T value.
  • method .reduce() introduces its own type variable, U. U expresses the fact that the following entities all have the same type (which you don’t need to specify, it is inferred automatically):
    • Parameter state of callback() (which is a function)
    • Result of callback()
    • Optional parameter firstState of .reduce()
    • Result of .reduce()

callback also gets a parameter element whose type has the same type T as the Array elements, a parameter index that is a number and a parameter array with T values.

Recommended TypeScript Resources

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