[英]TypeScript - Function return type based on multiple parameters
I'm creating a function that exports an image.我正在创建一个导出图像的函数。 It needs to take the
format
, exportType
and pathOrWithMetadata
parameters.它需要采用
format
、 exportType
和pathOrWithMetadata
参数。 I have the following code:我有以下代码:
interface ExportMetadata {
data: Buffer;
width: number;
height: number;
}
export type Format = "png" | "jpeg";
export type ExportTypes = "file" | "buffer";
export type Output<ExportType> = ExportType extends "file" ? undefined : Buffer;
export default async function exportTo<ExportType extends ExportTypes>(
image: Image,
format: Format,
exportType: ExportType,
pathOrWithMetadata?: string | boolean
): Promise<Output<ExportType>> { ... }
This will ensure that if the exportType
is "file"
, undefined
will be returned.这将确保如果
exportType
为"file"
,则将返回undefined
。 But if the exportType
is "Buffer"
, a Buffer
will be returned.但是如果
exportType
是"Buffer"
,则会返回一个Buffer
。 Now, I need the Buffer
return type to be dependent on the pathOrWithMetadata
parameter.现在,我需要
Buffer
返回类型依赖于pathOrWithMetadata
参数。 Firstly, if the exportType
is "file"
, pathOrWithMetadata
must be a string.首先,如果
exportType
是"file"
, pathOrWithMetadata
必须是一个字符串。 If the exportType
is "buffer"
, it needs to be a boolean.如果
exportType
是"buffer"
,它需要是一个布尔值。 If the boolean is true
, the return type needs to be ExportMetadata
.如果布尔值为
true
,则返回类型需要为ExportMetadata
。 If it's false
or undefined
, it needs to return a Buffer
.如果它是
false
或undefined
,它需要返回一个Buffer
。
Here's what I tried:这是我尝试过的:
interface ExportMetadata {
data: Buffer;
width: number;
height: number;
}
export type Format = "png" | "jpeg";
export type ExportTypes = "file" | "buffer";
export type PathOrWithMetadataOptions = string | boolean;
export type Output<ExportType, PathOrWithMetadata> = ExportType extends "file" ? undefined : (PathOrWithMetadata extends true ? ExportMetadata : Buffer);
export default async function exportTo<ExportType extends ExportTypes, PathOrWithMetadata extends PathOrWithMetadataOptions>(
image: Image,
format: Format,
exportType: ExportType,
pathOrWithMetadata?: PathOrWithMetadata
): Promise<Output<ExportType, PathOrWithMetadata>> { ... }
This worked for the following use cases:这适用于以下用例:
exportTo(image, "png", "file", "example.png"); // Promise<undefined>
exportTo(image, "png", "buffer", true); // Promise<ExportMetadata>
exportTo(image, "png", "buffer", false); // Promise<Buffer>
But didn't work when pathOrWithMetadata
is undefined
:但是当
pathOrWithMetadata
undefined
时pathOrWithMetadata
:
exportTo(image, "png", "buffer");
The return type here is Promise<ExportMetadata | Buffer>
这里的返回类型是
Promise<ExportMetadata | Buffer>
Promise<ExportMetadata | Buffer>
. Promise<ExportMetadata | Buffer>
. How can I have the return type be Promise<Buffer>
?如何让返回类型为
Promise<Buffer>
?
我发现解决方案是为PathOrWithMetadata
设置默认值, PathOrWithMetadata
所示:
export default async function exportTo<ExportType extends ExportTypes, PathOrWithMetadata extends PathOrWithMetadataOptions = false>(...): Promise<Output<ExportType, PathOrWithMetadata>> { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.