返回另一个函数的函数的返回类型是什么 [英] What is the return type for a function that returns another function

查看:22
本文介绍了返回另一个函数的函数的返回类型是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Typescript 开发 Protractor 测试.看来可用于量角器的 d.ts 文件已经过时了.我正在尝试更新它以包含预期条件量角器已添加.p>

总而言之,预期条件是量角器中的一组函数,它们返回一个返回值承诺的函数.

使用示例:

protractor.ExpectedCondtions.visibilityOf(element(by.id('button1')))();

我很困惑如何告诉量角器我正在返回一个将返回特定返回类型的函数.有人有这方面的经验吗?

解决方案

如果我理解正确,您的解决方案将取决于second"函数返回的类型.

简而言之,至少有两种方法可以做到:

  1. Lambda 语法
  2. 接口(普通和通用接口)

我已经尝试在下面的代码中解释所有这些,请检查一下:

模块主{导出类 TestClass{//使用lamba语法作为返回函数的接口受保护的 returnSpecificFunctionWhichReturnsNumber(): () =>数字{返回 this.specificFunctionWhichReturnsNumber;}protected specificFunctionWhichReturnsNumber(): number{返回0;}//使用接口来描述返回函数受保护的 returnSpecificInterfaceFunction(): INumberFunction{返回 this.specificFunctionWhichReturnsNumber;}//使用泛型接口来描述返回函数受保护的 returnSpecificGenericInterfaceFunction(): IReturnFunction<number>{返回 this.specificFunctionWhichReturnsNumber;}}//函数的接口,返回一个数字导出接口 INumberFunction{(): 数字;}//一个函数的通用接口,它返回一些东西导出接口 IReturnFunction{(): 值类型;}}

I am working on developing Protractor tests using Typescript. It appears that the d.ts file available for protractor is very out of date. I am trying to update it to include the Expected Conditions protractor has added.

To summarize it, Expected Conditions are a set of functions within protractor that return a function that returns a promise of your value.

An example of usage:

protractor.ExpectedCondtions.visibilityOf(element(by.id('button1')))();

I am stumped on how to tell protractor that I am returning a function that will return a specific return type. Does anyone have any experience with this?

解决方案

If I understood you correctly, your solution will depend on the type that the "second" function returns.

In a nutshell, there are at least 2 ways to do it:

  1. Lambda syntax
  2. Interfaces (normal and generic interfaces)

I've tried to explain all these in the code below, please, check it:

module main
{
    export class TestClass
    {
        // Use lamba syntax as an interface for a return function
        protected returnSpecificFunctionWhichReturnsNumber(): () => number
        {
            return this.specificFunctionWhichReturnsNumber;
        }

        protected specificFunctionWhichReturnsNumber(): number
        {
            return 0;
        }

        // Use an interface to describe a return function
        protected returnSpecificInterfaceFunction(): INumberFunction
        {
            return this.specificFunctionWhichReturnsNumber;
        }

        // Use a generic interface to describe a return function
        protected returnSpecificGenericInterfaceFunction(): IReturnFunction<number>
        {
            return this.specificFunctionWhichReturnsNumber;
        }
    }

    // An interface for a function, which returns a number
    export interface INumberFunction
    {
        (): number;
    }

    // A generic interface for a function, which returns something
    export interface IReturnFunction<ValueType>
    {
        (): ValueType;
    }
}

这篇关于返回另一个函数的函数的返回类型是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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