What is different between Pick<T, "properties"> and T["properties"] in typescript

Viewed 62

I'm typescript beginner.

What is difference between Pick and T["properties"] index reference?

For example:

interface Generic {
   red: number
   apple: string;
   
}

type RedType = Pick<Generic, "red">
type RedType2 = Generic["red"]
1 Answers

The first example picks only certain properties of an object and returns an object type

type RedType = Pick<Generic, 'red'>; // { red: number }

The second example gives you the type of the referenced property on an object

type RedType2 = Generic["red"]; // number
Related