Skip to Content
Learning TypeScript 2.x - Second Edition
book

Learning TypeScript 2.x - Second Edition

by Remo H. Jansen
April 2018
Beginner content levelBeginner
536 pages
13h 21m
English
Packt Publishing
Content preview from Learning TypeScript 2.x - Second Edition

Lookup types

Lookup types are another advanced type system feature that allow us to combine the keyof operator with generic and object literals to create advanced type annotations. Let's look at an example:

function filterByProperty<T, K extends keyof T>(    property: K, entities: T[], value: T[K]) {    return entities.filter(e => e[property] === value);}

The preceding function takes two generic type arguments:

  • T is the type of the items in the array passed as the first argument of the function.
  • K is the name of the properties of T. This requirement is enforced by a generic constraint (extends keyof T).

The function also expects two arguments:

  • An array of entities of type T.
  • A value of type T[K]. The type T[K] represents the type of the value ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering TypeScript - Fourth Edition

Mastering TypeScript - Fourth Edition

Nathan Rozentals
Learning TypeScript

Learning TypeScript

Josh Goldberg
TypeScript for Beginners

TypeScript for Beginners

Bharath Thippireddy

Publisher Resources

ISBN: 9781788391474Supplemental Content