首页 > 解决方案 > 打字稿推断返回对象

问题描述

我正在创建如下所示的设置函数:

type Setup<T> = (overrideProps?: Partial<T>) => void /* WHAT HERE */;

const setup: Setup<ProductProps> = (overrideProps) => {
  const props: ProductProps = {
    oldPrice: 9000,
    price: 8490,
    ...overrideProps,
  };

  const component = <Product {...props} />;

  return {
     component: testRenderer.create(component),
     props,
  };
};

我想输入可以不同的返回值,但有一件事是肯定的,函数将返回props变量,其他是可选的。

怎么打字?

标签: typescript

解决方案


推荐阅读