简体   繁体   English

在TypeScript中,如何根据其参数之一的返回类型来确定函数的返回类型?

[英]In TypeScript, how do I make a function's return type based on the return type of one of its arguments?

Is there some way I can have a function whose return type varies based on one of its arguments? 有什么方法可以让我的函数的返回类型根据其参数之一而有所不同? For example: 例如:

interface Person {
  name: string;
  job: string;
}

interface Animal {
  name: string;
  deadly: boolean;
}

function getPerson(): Person {
  return {
    name: 'Bob',
    job: 'Manager',
  };
}

function getAnimal(): Animal {
  return {
    name: 'Spot',
    deadly: false,
  };
}

function create(generator: () => Person | Animal): ReturnType<typeof generator>[] {
  return []
}

const people = create(getPerson);

This is kind of close, except the type of people is (Person | Animal)[] . 这是一种密切的,除了类型people(Person | Animal)[] I need it to be Person[] - in other words, it can look at the getPerson argument and know that this function will return a Person . 我需要它是Person[] -换句话说,它可以查看getPerson参数并知道此函数将返回Person I realize, of course, I could rewrite the function so I can do this: 我当然知道我可以重写该函数,所以我可以这样做:

const people = create<Person>(getPerson);

But I'd rather have it infer this from the arguments somehow. 但是我宁愿从争论中以某种方式推断出这一点。

Figured it out! 弄清楚了! I just modified this part of the above code: 我刚刚修改了上面代码的这一部分:

function create<M>(generator: () => M): M[] {
  return []
}

const people = create(getPerson);
const animals = create(getAnimal);

And that worked. 那行得通。

您已经弄清楚了,如果您想查找/了解更多信息,我只是想补充一下,该概念称为“泛型”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何让 typescript 根据 function2E977777777777777777777777777777777777777777777777777777777777777777777777777777777777BED18 的返回类型 functionC17A94F14Z 的返回类型来推断 function 的返回类型 - How do I get typescript to infer the return type of a function based on the return type on a function of its arguments 在TypeScript中,如何键入函数的参数而不是返回值? - In TypeScript, how do i type a function's arguments but not the return value? 使用 TypeScript 使函数的返回类型取决于其参数? - Make return type of function depends on its arguments with TypeScript? 如何基于参数中的函数返回类型指定打字稿条件类型 - How to specify typescript conditional type based on a function return type in the arguments Typescript 函数根据可选参数返回类型 - Typescript function return type based on optional arguments 如何让 TypeScript 根据返回值识别函数参数的类型? - How do I make TypeScript recognize the type of an argument to a function based on the return? 如何创建一个泛型 typescript 类型 function 并从参数类型推断出返回类型? - How do I make a generic typescript type function that has the return type inferred from the argument type? TypeScript 根据类型参数返回不同的类型 - TypeScript return different type based on type arguments 如何键入函数的 arguments 但保留返回类型“推断”? - How do I type a function's arguments but leave the return type "inferred"? 在 TypeScript 中调用一个以 union 作为返回类型的函数后,如何获得 vscode 自动完成功能? - How do I get vscode autocomplete after calling a function with a union as its return type in TypeScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM