Typescript 采用任何类型但不使用';t清除参数的形状信息

Typescript 采用任何类型但不使用';t清除参数的形状信息,typescript,Typescript,如何编写一个函数,它可以将任何类型作为参数并保留参数的类型信息 const shouldPreserveShapeInfo = (t: any) => t // put any because I want it to work with any shape const input = { foo: 'bar', john:'doe' } const result = shouldPreserveShapeInfo(input) // result is now of type

如何编写一个函数,它可以将任何类型作为参数并保留参数的类型信息

const shouldPreserveShapeInfo = (t: any) => t // put any because I want it to work with any shape

const input = {
  foo: 'bar',
  john:'doe'
}

const result = shouldPreserveShapeInfo(input)
// result is now of type any and lost the shape information of input
您可以使用:

const shouldPreserveShapeInfo=(t:t)=>t;
常量输入={
福:“酒吧”,
约翰:“多伊”
}
const result=shouldPreserveShapeInfo(输入)
//结果的类型为{foo:string,john:string}
您可以使用:

const shouldPreserveShapeInfo=(t:t)=>t;
常量输入={
福:“酒吧”,
约翰:“多伊”
}
const result=shouldPreserveShapeInfo(输入)
//结果的类型为{foo:string,john:string}

const shouldPreserveShapeInfo = <T>(t: T) => t;

const input = {
  foo: 'bar',
  john:'doe'
}

const result = shouldPreserveShapeInfo(input)
// result is of type { foo: string, john: string }