function fn(obj: Record<string, any>) {
return obj
}
const obj = { a: 1, b: 2, c: 3 }
// obj1's type: const obj1: Record<string, any>
const obj1 = fn(obj)
As you can see, I have a object with specific shape as argument for a function, and the function return a same shape object. What I want is get the return with most detailed shape(In my example is {a:number; b:number; c:number}
) rather than a commonly used type.
CodePudding user response:
That's simple:
function fn<T>(obj: T) {
return obj
}
const obj = { a: 1, b: 2, c: 3 }
// obj1's type: const obj1: {
// a: number;
// b: number;
// c: number;
// }
const obj1 = fn(obj)