如何在TypeScript中获取接口属性的类型?

如何在TypeScript中获取接口属性的类型?,typescript,Typescript,所以我有一个类似的代码: interface MyInterface{ a:any, b:string, c:boolean, d:number, readonly valueChanges:Subject<{key: keyof MyInterface, value: ???}> } 接口MyInterface{ a:有吗, b:绳子, c:布尔型, d:号码, 只读值更改:主题 } 我不知道如何在正确的“键”下写入值的类型。我尝试过MyInterface[

所以我有一个类似的代码:

interface MyInterface{
  a:any,
  b:string,
  c:boolean,
  d:number,
  readonly valueChanges:Subject<{key: keyof MyInterface, value: ???}>
}
接口MyInterface{
a:有吗,
b:绳子,
c:布尔型,
d:号码,
只读值更改:主题
}
我不知道如何在正确的“键”下写入值的类型。我尝试过MyInterface[key]的
类型,但可能这不是解决方法:(


提前感谢您的时间!

您可以使用发布的
AllValues
实用程序类型。请注意,您不需要
扩展记录,因为这是特定于反转问题的

type AllValues<T> = {
    [P in keyof T]: { key: P, value: T[P] }
}[keyof T]

Subject
的签名是什么?它是一个函数吗?@PritamKadam我不知道它为什么重要。它只是一个泛型-可能是一个类,也可能不是。我不知道它会如何改变任何答案。它可以这样定义,类型将被正确推断:
只读值更改:(k:k,v:MyInterface[k])=>void
type KeyValueObject = AllValues<Omit<MyInterface, "valueChanges">>
interface MyInterface{
  a: any;
  b: string;
  c: boolean;
  d: number;
  readonly valueChanges: Subject<AllValues<Omit<MyInterface, "valueChanges">>>;
}
interface BaseInterface {
  a: any;
  b: string;
  c: boolean;
  d: number;
}

type WithValueChanges<T> = T & {
    readonly valueChanges: Subject<AllValues<T>>
}

type MyInterface = WithValueChanges<BaseInterface>