将对象转换为返回相同对象的函数并继续转换

时间:2019-11-22 15:53:34

标签: typescript

我有这个对象:

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

我希望它将其转换为返回相同对象的函数,即

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

如何重新使用演员表,以便确保响应类型?

2 个答案:

答案 0 :(得分:0)

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

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

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

使用别名:

type FiveFunc = () => typeof five;

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

答案 1 :(得分:0)

据我所知,您可以做到

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

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

请告诉我您要搜索的是什么:)