Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an interface represents a tuple type in TypeScript?

Tags:

typescript

Glad to see the release of TypeScript 1.3, but how to write an interface represents a tuple type?

E.g.

var data: [string, number, number];

How to write an interface IData so that I would be able to do the same thing by writing

var data: IData;
like image 631
vilicvane Avatar asked Nov 14 '14 15:11

vilicvane


People also ask

How do you type a tuple in TypeScript?

TypeScript introduced a new data type called Tuple. Tuple can contain two values of different data types. Consider the following example of number, string and tuple type variables. var empId: number = 1; var empName: string = "Steve"; // Tuple type variable var employee: [number, string] = [1, "Steve"];

Is there a tuple in TypeScript?

TypeScript gives us a data type called tuple that helps to achieve such a purpose. It represents a heterogeneous collection of values. In other words, tuples enable storing multiple fields of different types. Tuples can also be passed as parameters to functions.

How do I return a tuple in TypeScript?

To declare a function with a tuple return type, set the return type of the function to a tuple right after the function's parameter list, e.g. function getTuple(): [number, number] {} . If the return type of the function is not set, TypeScript will infer it as type[] .

When should I use tuple TypeScript?

TypeScript tuples are like arrays with a fixed number of elements. They provide us with a fixed size container that can store values of multiple types, where the order and structure are very important. This data type is best used when we know exactly how many types we want to allow in an array.


3 Answers

I know this is an old question, but I think you can accomplish what you want with the following:

type IData = [string, number, number]; 

then

var data: IData; 

You can see this in this TypeScript Playground example

like image 122
Joe Skeen Avatar answered Sep 25 '22 00:09

Joe Skeen


Note that with some of the new features coming, such as union types, you can get roughly what you want. The latest draft of the spec contains an example along these lines (see https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.3.3 )

The below code shows an example of how this might look:

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

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

If you grab the latest code from the master branch and compile the above, you'll see where it detects the assignment to 'x' as valid, and the assignment to 'y' as an error:

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'.
like image 43
Bill Ticehurst Avatar answered Sep 27 '22 00:09

Bill Ticehurst


Little Late, but I think this approach is quite nice and simple and could help others:

type Type<T> = T;

interface IData extends Type<[number, number, number]>
{

}

var data: IData = ["Some Data", 42, 42];

This works also with Objects, if someone needs it:

type Type<T> = T;

interface IData extends Type<{ data: string }>
{

}

var data: IData = { data: "Some Data" };

You can also do this:

interface IData
{
    0: string;
    1: number;
    2: number;
}

var data: IData = ["Some Data", 42, 42];
like image 20
NextLegacy Avatar answered Sep 25 '22 00:09

NextLegacy