简体   繁体   English

如何基于参数中的函数返回类型指定打字稿条件类型

[英]How to specify typescript conditional type based on a function return type in the arguments

I have a function like this: 我有一个这样的功能:

export const getValue = (
  options: Options = { someMapping, someFunction }
): string | MyType => {
  ...
  return someFunction({ ...someData });
};

Right now, the return type is string or MyType. 现在,返回类型是字符串或MyType。

This is because someFunction can either return string or MyType. 这是因为someFunction可以返回字符串或MyType。

Is there a way to use TypeScript conditional type to return the correct type based on what someFunction actually returns? 有没有一种方法可以使用TypeScript条件类型根据someFunction实际返回的内容返回正确的类型?

[Additional Info after first post] : [第一篇文章后的其他信息]

Here is the definition of Options type: 这是选项类型的定义:

export interface Options {
  someMapping: MappingType;
  someFunc: (data: MyType) => MyType | string;
}

The someFunc param is a function that expects a MyType param. someFunc参数是需要MyType参数的函数。

Here are two actual functions I pass in depends on the situation: 根据情况,我传入了两个实际函数:

const myFunc = (data: MyType): string => {
  return data.someProperty;
};

const myFunc = (data: MyType): MyType => {
  return data;
};

I think the closest you can do is type someFunction and use function overloading. 我认为最接近的方法是键入someFunction并使用函数重载。 The Options makes it a little more complicated, but something like this should work: Options使它稍微复杂一些,但是这样的方法应该起作用:

type MyFunc<T extends MyType | string> = () => T;

interface Options<T extends MyType | string> {
  someMapping: WhateverTypeThisIs;
  someFunc: MyFunc<T>;
}

function getValue(options: Options<MyType>): MyType;
function getValue(options: Options<string>): string;
function getValue<T extends MyType | string>(options: Options<T>): T {
   return options.someFunc();
}

So now you should get typings for something like this: 所以现在您应该为以下内容输入内容:

const myTypeOptions: Options<MyType> = {
   someMapping: {},
   someFunc: () => ({ myParam: "MyValue" })
};
const myStringOptions: Options<string> = {
   someMapping: {},
   someFunc: () => "Hello"
};
const myTypeValue = getValue(myOptions); // myNumberValue is of type { myParam: string };
const myStringValue = getValue(myStringOptions); // myStringValue is a string;

This is of course dependent on the fact that the function in question always returns a MyType or always returns a string . 这当然取决于所讨论的函数始终返回MyType始终返回string的事实。 If it changes depending on the parameters of the function, then it will always be a MyType | string 如果它根据函数的参数而变化,则它将始终是MyType | string MyType | string because Typescript would not be able to determine that. MyType | string因为Typescript无法确定。 It would be up the caller to figure out what it is. 它将由调用者确定它是什么。

EDIT: 编辑:

Based on the scenario you presented, something like this might help. 根据您提出的方案,类似的方法可能会有所帮助。 You can create to types of MyFunc . 您可以创建MyFunc类型。

type MyTypeFunc = (myType: MyType) => MyType;
type MyStringFunc = (myType: MyType) => string;

interface MyOptions {
  someMapping: WatheverTypeThisIs;
}

interface MyTypeOptions extends MyOptions {
  someFunc: MyTypeFunc;
}

interface MyStringOptions extends MyOptions {
  someFunc: MyStringFunc;
}

function getValue(options: MyTypeOptions): MyType;
function getValue(options: MyStringOptions): string;
function getValue(options: MyTypeOptions | MyStringOptions): T {
   return options.someFunc();
}

Now, whenever you call it you can create or options like this: 现在,无论何时调用它,您都可以创建或类似以下的选项:

const myTypeOptions: MyTypeOptions = {
  someMapping: {},
  someFunc: (myType) => myType
}

const myStringOptions: MyStringOptions = {
  someMapping: {},
  someFunc: (myType) => myType.someProperty
}

const myTypeValue = getValue(myTypeOptions); // myTypeValue will be of type MyType
const myStringValue = getValue(myStringOptions); // myStringValue will be of type string

playground you can let typescript infer return type. 在Playground中,您可以让Typescript推断返回类型。 Function overloading provided by @DeeV works as well. @DeeV提供的函数重载也可以。

暂无
暂无

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

相关问题 Typescript:基于 arguments 道具的条件返回类型 - Typescript: Conditional return type based on arguments props Typescript 函数根据可选参数返回类型 - Typescript function return type based on optional arguments 我如何让 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:如何编写具有条件返回类型的函数 - TypeScript: How to write a function with conditional return type TypeScript 如何创建具有条件返回类型的函数? - TypeScript how to create a function with conditional return type? 在TypeScript中,如何根据其参数之一的返回类型来确定函数的返回类型? - In TypeScript, how do I make a function's return type based on the return type of one of its arguments? 如何根据输入类型在 typescript 中指定 promise 的返回类型 - How to specify return type of a promise in typescript based on the input type 基于 Typescript 中的参数类型的条件返回类型 - Conditional return type based on argument type in Typescript TypeScript 根据类型参数返回不同的类型 - TypeScript return different type based on type arguments 如何根据传递给 rest 参数 function 的 arguments 的数量指定不同的返回类型 - How to specify a different return type based on number of arguments passed to rest parameter function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM