TypeScript Tips and Tricks

Image of Author
Invalid Date (last updated October 20, 2022)

Creating Types from Types

You can access deeply nested implicit types.

interface A {
  list: { a: string; b: number }[]
}

function fun(item: A['list'][number]) {
  item.a
  item.b
}

// or

type B = A['list'][number]

Also, you don't even need a type

const A = {
  list: [
    { a: 'wow', b: 4 }
  ]
}

function fun(item: typeof A['list'][number]) {
  item.a
  item.b
}

type B = typeof A['list'][number]