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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26933365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:56:32  来源:igfitidea点击:

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

typescript

提问by vilicvane

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

很高兴看到 TypeScript 1.3 的发布,但是如何编写一个接口来表示元组类型?

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

如何编写接口 IData 以便我可以通过编写来做同样的事情

var data: IData;

回答by Joe Skeen

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 示例中看到这一点

回答by Bill Ticehurst

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:

如果您从 master 分支获取最新代码并编译上面的代码,您将看到它检测到对 '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'.

回答by Andrei Petrov

Much more boilerplateness approach than Joe Skeen's, but allows compile time type checks. And boilerplate util code write just once.. ;)

比 Joe Skeen 的样板方法多得多,但允许编译时类型检查。样板实用程序代码只编写一次..;)

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

回答by donnut

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