简体   繁体   English

如何在 TypeScript 中编写表示元组类型的接口?

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

Glad to see the release of TypeScript 1.3, but how to write an interface represents a tuple type?很高兴看到 TypeScript 1.3 的发布,但是如何编写一个接口来表示元组类型?

Eg例如

var data: [string, number, number];

How to write an interface IData so that I would be able to do the same thing by writing如何编写接口 IData 以便我可以通过编写来做同样的事情

var data: IData;

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 您可以在此TypeScript Playground示例中看到此信息

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 ) 该规范的最新草案包含这些行的示例(请参阅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: 如果从主分支中获取最新代码并编译上述代码,您将看到它将“x”的赋值检测为有效,并将“y”赋值为错误:

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'.

Much more boilerplateness approach than Joe Skeen's, but allows compile time type checks. 比Joe Skeen更多的样板方法,但允许编译时类型检查。 And boilerplate util code write just once.. ;) 和样板工具代码只写一次..;)

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

Can be used with arrays: 可以与数组一起使用:

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

You can not create an interface from a Tuple, like you can not make one from a string either. 你不能从元组创建一个接口,就像你不能从一个字符串中创建一个接口。

You can use a Tuple in an interface like: 您可以在以下界面中使用元组:

interface IDATA {
   value: [number, 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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM