如何在switch语句中缩小类型

近江

可以说我有以下代码

type Person = {
    race: string
}
type Animal = {
    species: string
}

export const adaptor = (
    input: Person | Animal,
    type: string
): string => {
    switch (type) {
        case 'person':
            return printRace(input); <-- Type error
        case 'animal':
            return printSpecies(input); <-- Type error
    }
};


function printRace(input: Person) {
    return input.name
}

function printSpecies(input: Animal) {
    return  input.species
}

当前我得到错误:

Argument of type 'Person | Animal' is not assignable to parameter of type 'Person'.
  Property 'name' is missing in type 'Animal' but required in type 'Person'.ts(2345)

在第一种情况下(另一种情况下类似的错误)。

我怎样才能printRace知道输入永远不会是类型Animal

目前,我可以实现的唯一方法是强制转换:

return printRace(input as Person);

还有另一种方法吗?

保罗·休恩

我认为将type直接包含PersonandAnimal类型中会更好好处是您不需要default在switch语句中包含a ,因为Typescript知道您已经提供了一个case的所有可能值input.type

type Person = {
    type: 'person',
    race: string
}
type Animal = {
    type: 'animal',
    species: string
}

export const adaptor = (
    input: Person | Animal,
): string => {
    switch (input.type) {
        case 'person':
            return printRace(input);
        case 'animal':
            return printSpecies(input);
    }
};


function printRace(input: Person) {
    return input.race
}

function printSpecies(input: Animal) {
    return input.species
}

操场

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Typescript-如何在switch语句中缩小泛型的类型可能性?

来自分类Dev

Javascript如何在if语句中缩小合适的URL

来自分类Dev

如何在此语句中解析类型

来自分类Dev

iOS swift 3 如何更改 switch 语句中的类型

来自分类Dev

如何在switch语句中使用通配符?

来自分类Dev

Twilio如何在switch语句中添加暂停

来自分类Dev

如何在switch语句中使用变量

来自分类Dev

如何在switch-case语句中自动缩进VSCode?

来自分类Dev

如何在嵌套的switch语句中显示图像?

来自分类Dev

如何在switch语句中使用随机生成的数字?

来自分类Dev

如何在switch语句中进行代码优化

来自分类Dev

如何在switch语句中处理可选内容

来自分类Dev

如何在switch语句中使用cin“作为参数”

来自分类Dev

如何在switch语句中使用Sql表数据?

来自分类Dev

如何在迭代器的collect语句中添加类型注释?

来自分类Dev

如何在此COMMON语句中指定类型?

来自分类Dev

Java如何在if语句中评估这种类型的代码?

来自分类Dev

如何在UPDATE语句中使用IF类型结构?

来自分类Dev

如何在条件语句中检查变量的类型

来自分类Dev

如何在一个switch语句中合并两个case语句

来自分类Dev

Clang-Format:如何在switch语句中获取单行case语句

来自分类Dev

switch语句中的Switch语句

来自分类Dev

如何在if语句中修复if语句?

来自分类Dev

存在类型类限制时,如何在实例语句中指定类型?

来自分类Dev

如何在switch语句中匹配正则表达式?

来自分类Dev

如何在基于组合返回值的switch语句中使用&&运算符?

来自分类Dev

如何在一条语句中使用Switch Map进行过滤和映射?

来自分类Dev

如何在Switch语句中使用C#枚举,同时保持其余代码相同

来自分类Dev

如何在switch语句中使用字符串资源

Related 相关文章

  1. 1

    Typescript-如何在switch语句中缩小泛型的类型可能性?

  2. 2

    Javascript如何在if语句中缩小合适的URL

  3. 3

    如何在此语句中解析类型

  4. 4

    iOS swift 3 如何更改 switch 语句中的类型

  5. 5

    如何在switch语句中使用通配符?

  6. 6

    Twilio如何在switch语句中添加暂停

  7. 7

    如何在switch语句中使用变量

  8. 8

    如何在switch-case语句中自动缩进VSCode?

  9. 9

    如何在嵌套的switch语句中显示图像?

  10. 10

    如何在switch语句中使用随机生成的数字?

  11. 11

    如何在switch语句中进行代码优化

  12. 12

    如何在switch语句中处理可选内容

  13. 13

    如何在switch语句中使用cin“作为参数”

  14. 14

    如何在switch语句中使用Sql表数据?

  15. 15

    如何在迭代器的collect语句中添加类型注释?

  16. 16

    如何在此COMMON语句中指定类型?

  17. 17

    Java如何在if语句中评估这种类型的代码?

  18. 18

    如何在UPDATE语句中使用IF类型结构?

  19. 19

    如何在条件语句中检查变量的类型

  20. 20

    如何在一个switch语句中合并两个case语句

  21. 21

    Clang-Format:如何在switch语句中获取单行case语句

  22. 22

    switch语句中的Switch语句

  23. 23

    如何在if语句中修复if语句?

  24. 24

    存在类型类限制时,如何在实例语句中指定类型?

  25. 25

    如何在switch语句中匹配正则表达式?

  26. 26

    如何在基于组合返回值的switch语句中使用&&运算符?

  27. 27

    如何在一条语句中使用Switch Map进行过滤和映射?

  28. 28

    如何在Switch语句中使用C#枚举,同时保持其余代码相同

  29. 29

    如何在switch语句中使用字符串资源

热门标签

归档