Flow使用

  • Flow开发工具插件 Flow-lanuage(vs code 商店中搜索)

    https://flow.org/en/docs/editors/ 关于lflow的插件
  • Flow原始类型
     
    //flow原始类型
    
    const a: string = 'foo'
    
    const b: number = Infinity // NaN // 100
    
    const c: Boolean = true // false
    
    const d: null = null
    
    const e: void = undefined
    
    const f: symbol = Symbol
    
    //Flow 数组类型
    
    const arr1: Array<number> = [1, 2, 3, 4]
    
    const arr1: number[] = [1, 2, 3, 4]
    
    //元组
    const foo: [String, Number] = ['foo', 100]
    
    //对象类型
    const obj1 = { foo: string, bar: number} = { foo:'string', bar:100}
    
    const obj2 = { foo?: string, bar: number} = { bar: 100 }
    
    const obj3 = { [string]: string } = {}
    
    obj3.key1 = 'value1'
    obj3.key1 = 'value2'
    
    
    
    //函数类型
    
    function foo (callback: (string, number) => void) {
        callback('string', 100)
    }
    
    foo(function (str, n) {
    
    })
    
    //特殊类型
    const a: 'foo' = 'foo'
    
    const type: 'success' | 'warning' | 'danger' = 'success'
    
    const StringOrNumber = string | number
    
    const b: StringOrNumber = 'string' //100
    
    const gender: ?number = undefined
    
    const gender: number | null | void = undefined
    
    
    
    
    //mixed 和 any 都可以传入 string number boolean...
    //二者区别是,mixed 是强类型,安全,any 是若类型,使用不安全,少用,它存在的意义是为了兼容以前的旧代码
    function passMixed (value: mixed) {
        if (typeof value === 'string') {
            value.substr(1)
        }
    
        if (typeof value === 'number') {
            value * value
        }
    }
    
    passMixed('string')
    passMixed(100)
    
    // ===================
    
    function passAny (value: any){
        value.substr(1)
    
        value * value
    }
    
    passMixed('string')
    passMixed(100)

    Flow文档网址
    https://flow.org/en/docs/types/  
    https://www.saltycrane.com/cheat-sheets/flow-type/latest/

     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值