在TypeScript中,如何声明接受字符串并返回字符串的函数数组?

时间:2021-02-18 07:21:51

UPDATE - the context of this question was pre-TypeScript 1.4. Since that version, my first guess has been supported by the language. See the update to the answer.

更新 - 此问题的上下文是预先TypeScript 1.4。从那个版本开始,我的第一个猜测得到了语言的支持。请参阅答案的更新。


I can declare f to be a function that accepts a string and returns a string:

我可以声明f是一个接受字符串并返回一个字符串的函数:

var f : (string) => string

And I can declare g to be an array of string:

我可以声明g是一个字符串数组:

var g : string[]

How can I declare h to be an array of "function that accepts a string and returns a string"?

如何将h声明为“接受字符串并返回字符串的函数”的数组?

My first guess:

我的第一个猜测:

var h : ((string) => string)[]

That seems to be a syntax error. If I take away the extra parentheses then it's a function from string to array of string.

这似乎是一个语法错误。如果我拿走额外的括号,那么它是一个从字符串到字符串数组的函数。

2 个解决方案

#1


38  

I figured it out. The problem is that the => for a function type literal is itself merely syntactic sugar and doesn't want to compose with [].

我想到了。问题是函数类型文字的=>本身只是语法糖,不想用[]组成。

As the spec says:

正如规范所说:

A function type literal of the form

表单的函数类型文字

( ParamList ) => ReturnType

(ParamList)=> ReturnType

is exactly equivalent to the object type literal

完全等同于对象类型文字

{ ( ParamList ) : ReturnType }

{(ParamList):ReturnType}

So what I want is:

所以我想要的是:

var h : { (s: string): string; }[]

Complete example:

完整的例子:

var f : (string) => string

f = x => '(' + x + ')';

var h : { (s: string): string; }[]

h = [];

h.push(f);

Update:

更新:

Judging from this changeset parentheses will be allowed in type declarations in 1.4, so the "first guess" in the question will also be correct:

从这个变更集判断,在1.4中的类型声明中将允许使用括号,因此问题中的“第一个猜测”也是正确的:

var h: ((string) => string)[]

Further Update It is in 1.4!

进一步更新它在1.4!

#2


0  

Based on your research I wrote a little class PlanetGreeter/SayHello:`

根据你的研究,我写了一个小类PlanetGreeter / SayHello:`

/* PlanetGreeter */

class PlanetGreeter {
    hello    : { () : void; } [] = [];
    planet_1 : string = "World";
    planet_2 : string = "Mars";
    planet_3 : string = "Venus";
    planet_4 : string = "Uranus";
    planet_5 : string = "Pluto";
    constructor() {
        this.hello.push( () => { this.greet(this.planet_1); } );
        this.hello.push( () => { this.greet(this.planet_2); } );
        this.hello.push( () => { this.greet(this.planet_3); } );
        this.hello.push( () => { this.greet(this.planet_4); } );
        this.hello.push( () => { this.greet(this.planet_5); } );
    } 
    greet(a: string): void { alert("Hello " + a); }
    greetRandomPlanet():void { 
        this.hello [ Math.floor( 5 * Math.random() ) ] (); 
    } 
} 
new PlanetGreeter().greetRandomPlanet();

#1


38  

I figured it out. The problem is that the => for a function type literal is itself merely syntactic sugar and doesn't want to compose with [].

我想到了。问题是函数类型文字的=>本身只是语法糖,不想用[]组成。

As the spec says:

正如规范所说:

A function type literal of the form

表单的函数类型文字

( ParamList ) => ReturnType

(ParamList)=> ReturnType

is exactly equivalent to the object type literal

完全等同于对象类型文字

{ ( ParamList ) : ReturnType }

{(ParamList):ReturnType}

So what I want is:

所以我想要的是:

var h : { (s: string): string; }[]

Complete example:

完整的例子:

var f : (string) => string

f = x => '(' + x + ')';

var h : { (s: string): string; }[]

h = [];

h.push(f);

Update:

更新:

Judging from this changeset parentheses will be allowed in type declarations in 1.4, so the "first guess" in the question will also be correct:

从这个变更集判断,在1.4中的类型声明中将允许使用括号,因此问题中的“第一个猜测”也是正确的:

var h: ((string) => string)[]

Further Update It is in 1.4!

进一步更新它在1.4!

#2


0  

Based on your research I wrote a little class PlanetGreeter/SayHello:`

根据你的研究,我写了一个小类PlanetGreeter / SayHello:`

/* PlanetGreeter */

class PlanetGreeter {
    hello    : { () : void; } [] = [];
    planet_1 : string = "World";
    planet_2 : string = "Mars";
    planet_3 : string = "Venus";
    planet_4 : string = "Uranus";
    planet_5 : string = "Pluto";
    constructor() {
        this.hello.push( () => { this.greet(this.planet_1); } );
        this.hello.push( () => { this.greet(this.planet_2); } );
        this.hello.push( () => { this.greet(this.planet_3); } );
        this.hello.push( () => { this.greet(this.planet_4); } );
        this.hello.push( () => { this.greet(this.planet_5); } );
    } 
    greet(a: string): void { alert("Hello " + a); }
    greetRandomPlanet():void { 
        this.hello [ Math.floor( 5 * Math.random() ) ] (); 
    } 
} 
new PlanetGreeter().greetRandomPlanet();