Skip to content

Instantly share code, notes, and snippets.

@danwbyrne
Last active August 3, 2018 18:34
Show Gist options
  • Save danwbyrne/baf1b8e357356ad8093eb010c6395834 to your computer and use it in GitHub Desktop.
Save danwbyrne/baf1b8e357356ad8093eb010c6395834 to your computer and use it in GitHub Desktop.
Typescript typing recipes

Useful Type Rules That Saves You Looking It Up

Reduce

let count = -1;
const array = ReadonlyArray<T>;

array.reduce<{[k in string]: T}>((acc: {[k in string]: T}, entry) => ({
  //              ^-->acc-type               ^--->acc-type
  count = count + 1;
  return {
    ...acc,
    [count]: array[count]
  }
}, {})

The count stuff is all bad and I can be ignored, whats important here is the typing for acc. I'm pretty sure you don't need it typed in both places.

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