打字稿返回类型取决于参数 [英] Typescript return type depending on parameter

查看:27
本文介绍了打字稿返回类型取决于参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数,该函数采用布尔类型的参数并根据输入的值返回两种类型之一.我找到了两种方法:

I am trying to write a function which takes a parameter of type boolean and returns one of two types, depending on the value of the input. I have found two approaches:

function dependsOnParameter<B extends boolean>(x: B): B extends true ? number : string {
    if (x) {
        return 3;
    } else {
        return "string";
    }
}

这里,TypeScript 说 Type '3'/'"string"' 不能分配给类型 'B extends true ?数字:字符串'.

Here, TypeScript says that Type '3'/'"string"' is not assignable to type 'B extends true ? number : string'.

我的另一种方法是这样的:

My other approach looks like this:

function dependsOnParameter(x: true): number;
function dependsOnParameter(x: false): string;
function dependsOnParameter(x: boolean): number | string {
    if (x) {
        return 3;
    } else {
        return "string";
    }
}

这样编译;但是,如果我尝试使用我的功能:

This compiles; however, if I try to use my function:

function calling(x: boolean) {
    dependsOnParameter(x);
}

我得到 'boolean' 类型的参数不能分配给 'false' 类型的参数.

有没有什么方法可以在不使用 any 的情况下实现我想要的?

Is there any way to achieve what I want without using any?

推荐答案

这两种方法都是有效的.如果您的函数在返回中使用条件类型,则需要使用类型断言,因为 typescript 不会尝试推理条件类型,因为它包含一个自由类型参数:

Both approaches are valid. If your function uses conditional types in the return it will need to use type assertions, as typescript will not try to reason about the conditional type since it contains a free type parameter:

function dependsOnParameter<B extends boolean>(x: B): B extends true ? number : string {
    if (x) {
        return 3 as any;
    } else {
        return "string"as any;
    }
}

这种方法使用了你想避免的any.

This approach uses any which you want to avoid.

第二种方法,我们无需求助于类型断言,只需复制最后一个签名即可:

The second approach we can get to work without resorting to type assertions by just duplicating the last signature:

function dependsOnParameter(x: true): number;
function dependsOnParameter(x: false): string;
function dependsOnParameter(x: boolean): number | string
function dependsOnParameter(x: boolean): number | string {
    if (x) {
        return 3;
    } else {
        return "string";
    }
}

function calling(x: boolean) {
    dependsOnParameter(x); // returns number| string
    dependsOnParameter(true); // returns number
    dependsOnParameter(false); // returns string
}

最后一个签名是实现签名,不可公开访问.您可以通过复制它来访问它.编译器不够聪明,无法将两个重载与 true/false 结合起来,并决定返回类型是 string|number

The last signature is the implementation signature and is not publicly accessible. You can make it accessible by duplicating it. The compiler is not smart enough to combine the two overloads with true/false and decide the return type is string|number

编辑

我们也可以结合这两种方法来减少签名:

We can also combine the two approaches for fewer signatures:

function dependsOnParameter<B extends boolean>(x: B): B extends true ? number : string 
function dependsOnParameter(x: boolean): number | string{
    if (x) {
        return 3;
    } else {
        return "string";
    }
}

这篇关于打字稿返回类型取决于参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆