[英]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.