Typescript Notes

we can get the "nice and short" error message mentioning out Banana type if we use interface instead of type

Array of objects

Here's an object of type List

interface List {
  id: number
  title: string
}

And we want an array Lists of objects List, which can be done the following two ways:

using an indexer

// using an indexer
interface Lists {
  [index: number]: List
}

and my preferred one

interface Lists extends Array<List> {}

And now we can declare the interface like so

// Option A
var result: List[] = [
  { id: 0, label: 'CId', key: 'contentId' },
  { id: 1, label: 'Modified By', key: 'modifiedBy' },
  { id: 2, label: 'Modified Date', key: 'modified' },
  { id: 3, label: 'Status', key: 'contentStatusId' },
  { id: 4, label: 'Status > Type', key: ['contentStatusId', 'contentTypeId'] },
  { id: 5, label: 'Title', key: 'title' },
  { id: 6, label: 'Type', key: 'contentTypeId' },
  { id: 7, label: 'Type > Status', key: ['contentTypeId', 'contentStatusId'] },
]

// Option B
var result: Lists = [
  { id: 0, label: 'CId', key: 'contentId' },
  { id: 1, label: 'Modified By', key: 'modifiedBy' },
  { id: 2, label: 'Modified Date', key: 'modified' },
  { id: 3, label: 'Status', key: 'contentStatusId' },
  { id: 4, label: 'Status > Type', key: ['contentStatusId', 'contentTypeId'] },
  { id: 5, label: 'Title', key: 'title' },
  { id: 6, label: 'Type', key: 'contentTypeId' },
  { id: 7, label: 'Type > Status', key: ['contentTypeId', 'contentStatusId'] },
]

Links

Please note that this site and the posts on it are, and will always be, a work in progress. If i waited for perfection, i’d never get anything done.