美文网首页
TypeScript中是使用强类型函数作为参数

TypeScript中是使用强类型函数作为参数

作者: _sands | 来源:发表于2019-03-28 17:14 被阅读0次
    class Foo {
        save(callback: (n: number) => any) : void {
            callback(42)
        }
    
        multipleCallbacks(firstCallback: (s: string) => void, secondCallback: (b: boolean) => boolean): void {
            firstCallback("hello world")
    
            let result: boolean = secondCallback(true)
            console.log("Resulting boolean: " + result)
        }
    }
    
    使用
    
    var foo = new Foo()
    
    foo.save((newNumber: number) => {
        console.log("Some number: " + newNumber)
    
        // This is optional, since "any" is the declared return type.
        return newNumber
    })
    
    // Multiple callbacks example.
    // Each call is on a separate line for clarity.
    // Note that `firstCallback()` has a void return type, while the second is boolean.
    foo.multipleCallbacks(
        (s: string) => {
             console.log("Some string: " + s)
        },
        (b: boolean) => {
            console.log("Some boolean: " + b)
            let result = b && false
    
            return result
        }
    )
    
    

    相关文章

      网友评论

          本文标题:TypeScript中是使用强类型函数作为参数

          本文链接:https://www.haomeiwen.com/subject/hxdcbqtx.html