您如何在 Typescript 中为函数就地解构编写类型?

How do you write the types for a function in-place destructuring in Typescript?

用户将 hisObject 传递给 myFn。看这个简单的例子:

type HisType = { a:string, b:number };

function myFn( { a:string, b:number } = hisObject): void {
  console.log(a,b);
}

但是我们可以包括 hisObjectHisType 类型以避免错误吗?

没有默认参数:

function myFn({ a, b }: HisType): void {
  console.log(a, b);
}

使用默认参数(hisObject指向默认值):

// This exists earlier in the program
const hisObject: HisType = /* ... */;

function myFn({ a, b }: HisType = hisObject): void {
  console.log(a, b);
}