Typescript 将对象转换为返回相同对象并保持强制转换的函数

Typescript 将对象转换为返回相同对象并保持强制转换的函数,typescript,Typescript,我有一个目标: const five: { amount: number } = { amount: 5, } 我希望它将其转换为返回相同对象的函数,即 const five = () => ({amount: 5}) 如何重新使用强制转换以确保响应类型?您可以使用typeof运算符查询对象类型: const five: { amount: number } = { amount: 5, } const fiveFunc: () => typeof five = (

我有一个目标:

const five: { amount: number } = {
  amount: 5,
}
我希望它将其转换为返回相同对象的函数,即

const five = () => ({amount: 5})

如何重新使用强制转换以确保响应类型?

您可以使用
typeof
运算符查询对象类型:

const five: { amount: number } = {
    amount: 5,
}

const fiveFunc: () => typeof five = () => ({ amount: 1 });
别名为:

type FiveFunc = () => typeof five;

const fiveFunc: FiveFunc = () => ({ amount: 1 });

据我所知,我认为你能做到

const five: { amount: number } = {
  amount: 5,
}

let x=new Function()
{ () => ({amount: 5})}

请让我知道这是否是您正在搜索的:)

您的意思是限制函数返回与原始对象相同的类型吗?@AlekseyL。确切地