Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarification on index type query operator and extends keyword

Tags:

typescript

I was going over the documentation on Advanced Types in Typescript (found here: https://www.typescriptlang.org/docs/handbook/advanced-types.html) and it mentioned the Index Query Operator and gave the following as an example:

function pluck<T, K extends keyof T>(o: T, names: K[]): T[K][] {
  return names.map(n => o[n]);
}

I understand how keyof produces a union of all known public property names on object T but I don't exactly understand what role the extends plays. I get that it's saying that K must be a valid property of T but why is extends used and not something else? Also, is it possible to extend unions in Typescript or is this more of a semantic specific to generics?

Thanks.

like image 967
Willwsharp Avatar asked Sep 27 '17 01:09

Willwsharp


People also ask

What are the different types of keyword queries?

Keyword Queries : Simplest and most common queries. The user enters just keyword combinations to retrieve documents. These keywords are connected by logical AND operator. All retrieval models provide support for keyword queries. 2. Boolean Queries :

What are keywords and keywords in indexing?

During the process of indexing, many keywords are associated with document set which contains words, phrases, date created, author names, and type of document. They are used by an IR system to build an inverted index which is then consulted during the search.

When does the query plan begin to use the index?

That means the query plan, the plan that SQL creates when determining the best way to perform a query, will begin to use the index when queries are being made. Notice that “friends_pkey” is listed as an index even though we never declared that as an index.

Can'key'be used as an index type?

Type 'key' cannot be used as an index type. 'key' refers to a value, but is being used as a type here. Did you mean 'typeof key'? The TypeScript docs are an open source project. Help us improve these pages by sending a Pull Request ❤


1 Answers

The keyword extends, in this context, is a way to add a constraint on a type parameter.

Given this simple example, you will see the compiler only allows you to pass in objects that conform to the Person interface:

interface Person {
    name: string;
}

function logAndReturnPerson<T extends Person>(person: T) {
    console.log(person.name);
    return person;
}

// ok
const result1 = logAndReturnPerson<Person>({ name: "David" });
// not ok, string does not satisfy constraint of Person
const result2 = logAndReturnPerson<string>("");

In your example, K extends keyof T means that K is constrained to keyof T. In other words, this means what you thought—K is a string that is constrained to be a property name of T.

Yes, this is a semantic specific to generics. Note that it's possible to use a union type in a constraint.

like image 197
David Sherret Avatar answered Oct 07 '22 13:10

David Sherret