Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

typescript

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"]
like image 281
dante Avatar asked Oct 31 '25 22:10

dante


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
like image 80
Passersby Avatar answered Nov 02 '25 13:11

Passersby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!