How to get type by using object property in generic typescript

Viewed 20

How can I get type by object property from generic?
What I want to do is ▼

type A = {data:string, name: string};

interface B<T> {
  data: T['data'] // error.. want to be expected as string
  ...
}
1 Answers

TypeScript does not know that T should have a property data. A constraint would fix this.

type A = { data:string, name: string };

interface B<T extends { data: any }>{
  data: T['data'] 
}
Related