如何从typeof创建接口?

Ray*_*ang 3 javascript typescript

如何从对象动态“向后”到接口?

const o = {
  a: 1,
  b: "hi"
}

interface i = typeof o; // how to write this?
Run Code Online (Sandbox Code Playgroud)

结果应等同于:

interface i {
  a: number,
  b: string
}
Run Code Online (Sandbox Code Playgroud)

Tit*_*mir 5

你不能直接创建一个接口,但你可以创建一个类型别名,在大多数情况下你可以像一个接口一样使用它(即你可以从它扩展一个新接口或在一个类中实现它

const o = {
a: 1,
b: "hi"
}

type i = typeof o; 
interface ii extends i { }
class ci implements i {
    a = 1;
    b = ''
}
Run Code Online (Sandbox Code Playgroud)