gpt4 book ai didi

casting - 如何强制Flow将值强制转换为另一种类型?

转载 作者:行者123 更新时间:2023-12-03 09:45:05 26 4
gpt4 key购买 nike

是否可以在Flow中强制转换变量?

type StringOrNumber = string | number
const foo: StringOrNumber = 'hello'

// I look for something like `const bar:string = (string) foo`
const bar: string = foo // fails
const bar: string = (foo: string) // also fails

最佳答案

Flow不会将类型从一种直接转换为另一种,但是您可以执行类似的操作

const bar: string = (foo: any);

因此您将 foo转换为 any,因为 any接受任何类型的值作为输入。然后,因为 any类型还允许您从中读取所有可能的类型,所以您可以将 any值分配给 bar,因为 any也是 string

关于casting - 如何强制Flow将值强制转换为另一种类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41328728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com