typescript 返回另一个函数的函数的返回类型是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33923278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the return type for a function that returns another function
提问by jordan
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. (http://www.protractortest.org/#/api?view=ExpectedConditions) To summarize it, Expected Conditions are a set of functions within protractor that return a function that returns a promise of your value.
我正在使用 Typescript 开发量角器测试。看来可用于量角器的 d.ts 文件已经过时了。我正在尝试更新它以包括添加的预期条件量角器。( http://www.protractortest.org/#/api?view=ExpectedConditions) 总而言之,预期条件是量角器中的一组函数,它们返回一个函数,该函数返回您的值的承诺。
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?
我很难过如何告诉量角器我正在返回一个将返回特定返回类型的函数。有人对这个有经验么?
采纳答案by Mark Dolbyrev
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:
简而言之,至少有两种方法可以做到:
- Lambda syntax
- Interfaces (normal and generic interfaces)
- Lambda 语法
- 接口(普通和通用接口)
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;
}
}
回答by andras
Since this question popped up first in Google for how to type return function for a function that returns a function, I will add the generic solution here for declaring these types.
由于这个问题首先在 Google 中出现,关于如何为返回函数的函数键入返回函数,因此我将在此处添加通用解决方案来声明这些类型。
So if you want to add type declaration to this curried add
function:
所以如果你想给这个柯里化add
函数添加类型声明:
const add = (a : number) => (b: number) => a + b;
You just duplicate what is after the =
sign and make the return value the corresponding value:
您只需复制=
符号后面的内容并使返回值成为相应的值:
export const add: (a : number) => (b: number) => number =
(a : number) => (b: number) => a + b;
But at this point, you don't need the types for the actual function, so you can just type this, as if it was JS:
但是此时,您不需要实际函数的类型,因此您只需键入以下内容,就好像它是 JS 一样:
export const add: (a : number) => (b: number) => number =
a => b => a + b;
Writing it more extensively:
写得更广泛:
const add: (a : number) => (b: number) => number =
a => {
console.log(a);
return b => {
console.log(b);
return a + b;
}
};
With generics:
使用泛型:
export const add: <A extends number, B extends number>(a : A) => (b: B) => number =
a => b => a + b;