에서 호환되지 않습니 어떻게 선언하는 배열의 기능이 허용하는 문자열과 문자열을 반환?

StackOverflow https://stackoverflow.com//questions/12706161

  •  13-12-2019
  •  | 
  •  

문제

업데이트 -컨텍스트의 이 질문은 사전 타이프 라이터 1.4.이후 버전에 내 첫 번째 추측되었습에 의해 지원되는 언어입니다.조 업데이트하는 대답이다.


나는 선언 할 수 있습니다 f 을 수있는 기능을 허용하는 문자열과 문자열을 반환합니다:

var f : (string) => string

나는 선언 할 수 있습니다 g 할 문자열:

var g : string[]

할 수 있는 방법을 선언 h 을 배열의"기능을 허용하는 문자열과 문자열을 반환합니다"?

내 첫 번째 추:

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

가 될 것으로 보인 구문 오류가 있습니다.면 나는 멀리 여분의 괄호는 다음의 함수에서 문자열을 배열의 문자열입니다.

도움이 되었습니까?

해결책

나는 그것을 알아 냈다.문제는 => 함수에 대한 형식 문자 자체 단순한 구문 설탕을 원하지 않는 구성 [].

로 spec 말한다:

기능 유형 리터럴의 양식

(ParamList)=>ReturnType

해당하는 객체형 리터럴

{(ParamList):ReturnType}

그래서 내가 원하는:

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

전체 예제:

var f : (string) => string

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

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

h = [];

h.push(f);

업데이트:

에서 심사 이 변경 집합 괄호 안에서 허용되는 유형의 선언 1.4,그래서"첫째로 추측"질문에서도 정확:

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

추가 업데이트 그것은에서 1.4!

다른 팁

조사를 기반으로 작은 수준의 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();
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top