2014-11-14 10 views

Odpowiedz

36

wiem, że to jest stare pytanie, ale myślę, że możesz osiągnąć to, co chcesz, za pomocą:

type IData = [string, number, number]; 

następnie

var data: IData; 

Można to zobaczyć w tym TypeScript Playground example

1

Nie można utworzyć interfejs z krotka, jak nie można wykonać jedną z ciąg albo.

Można użyć krotka w interfejsie jak:

interface IDATA { 
    value: [number, string]; 
} 
+0

Stosując tę ​​deklarację inteface nie daje rodzaj wsparcia dla wartości wewnątrz krotki. : '( –

11

Należy pamiętać, że niektóre z nowych funkcji nadchodzi, takie jak typy związków, można uzyskać mniej więcej to, co chcesz. Najnowszy projekt spec zawiera przykład wzdłuż tych linii (patrz https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.3.3)

Poniższy kod pokazuje przykład jak to może wyglądać:

interface KeyValuePair extends Array<string | number> { 0: string; 1: number; } 

var x: KeyValuePair = ["test", 42]; // OK 
var y: KeyValuePair = [42, "test"]; // Error 

Jeśli chwycić najnowszy kod z gałęzi głównej i skompilować powyższego, zobaczysz gdzie wykrywa przypisanie do „x” jako ważny, a przypisanie do „Y” jako błąd:

S:\src\TypeScript\bin>node tsc c:\temp\tuple.ts 
c:/temp/tuple.ts(4,5): error TS2323: Type '[number, string]' is not assignable to type 'KeyValuePair'. 
    Types of property '0' are incompatible. 
    Type 'number' is not assignable to type 'string'. 
+0

To jest bardzo dobre rozwiązanie, aby zagwarantować sprawdzanie typu wewnątrz krotki podczas kompilacji Kciuki w górę Niestety, WebStorm 2016.2.3 oznacza ten kod jako nieważny, ale obsługa WebScriptu WebStorm jest zastrzeżona, więc jest to po prostu błąd WebStorm. . :) –

+0

Nie jest 'interfejs KeyValuePair rozszerza Array {0: ciąg; 1: numer; } ' to samo, co przy powiedzeniu (z niestandardowym typem krotki): ' type KeyValuePair = [ciąg, liczba] '? –

1

znacznie więcej niż podejście boilerplateness Joe Skeen, ale pozwala kompilować kontrole typu TIME. i kod zapisu boilerplate util tylko raz ..;)

function usage(t: CortegeOf2<boolean, string>) { 
    get1(t).toLowerCase(); //ok 

    // var trash1 = t[2]; //runtime error 
    // var e0 = get2(t); //compile-time error we cannot get 2nd element cuz t has only 0th and 1st 

    // var trash2: string = t[1]; //sadly that syntax allows to pass value somewhere, where expected another type 
    // trash2.toUpperCase(); //runtime error 

    // var e2: string = get1(t); //but that usage will not allow that pass 
} 


export interface CortegeOf1<T0> { 
    0: T0; 
} 

export interface CortegeOf2<T0, T1> extends CortegeOf1<T0> { 
    1: T1; 
} 

export interface CortegeOf3<T0, T1, T2> extends CortegeOf2<T0, T1> { 
    2: T2; 
} 

export function get0<T>(cortege: CortegeOf1<T>): T { 
    return cortege[0]; 
} 

export function get1<T>(cortege: CortegeOf2<any, T>): T { 
    return cortege[1]; 
} 

export function get2<T>(cortege: CortegeOf3<any, any, T>): T { 
    return cortege[2]; 
} 

Może być stosowany z tablicami:

export function joinTwo<A, B>(a: Promise<A>, b: Promise<B>): Promise<CortegeOf2< A, B >> { 
    return Promise.all([a, b]); 
} 

function joinThree<A, B, C>(a: Promise<A>, b: Promise<B>, c: Promise<C>): Promise<CortegeOf3< A, B, C >> { 
    return Promise.all([a, b, c]); 
} 
Powiązane problemy