2014-11-14 15 views

risposta

36

so che questa è una vecchia questione, ma vi pensate può realizzare ciò che vuoi con il seguente:

type IData = [string, number, number]; 

poi

var data: IData; 

si può vedere questo in questo TypeScript Playground example

1

Non è possibile creare un'interfaccia da una tupla, come non si può fare uno da una stringa o.

È possibile utilizzare una tupla in un'interfaccia simile:

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

Usando questa dichiarazione inteface non dà sostegno tipo per i valori all'interno della tupla. : '( –

11

Si noti che con alcune delle novità in arrivo, come ad esempio i tipi di unione, è possibile ottenere più o meno quello che vuoi. L'ultima bozza della specifica contiene un esempio in questo senso (vedi https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.3.3)

Il codice seguente mostra un esempio di come questo potrebbe apparire:

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

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

Se si afferra l'ultimo codice dal ramo principale e compilare il sopra, vedrete dove si rileva il compito di 'x' come valido, e l'assegnazione a 'y' come un errore:

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

Questa è un'ottima soluzione per garantire i controlli di tipo all'interno della tupla durante la compilazione.) Thumbs up! Sfortunatamente, WebStorm 2016.2.3 contrassegna questo codice come non valido ma il supporto TypeScript di WebStorm è propizio, quindi è solo colpa di WebStorm. . :) –

+0

non è l'interfaccia 'KeyValuePair estende l'array {0: stringa; 1: numero; } come dire (con un tipo di tupla personalizzato): 'type KeyValuePair = [stringa, numero]'? –

1

Molto più approccio boilerplateness di Joe Skeen di, ma permette la compilazione controlli di tipo tempo. E codice standard util scrivere solo una volta ..;)

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]; 
} 

Può essere utilizzato con gli array:

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]); 
} 
Problemi correlati