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;
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"];
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.
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[] .
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.
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
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'.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With