方括号是什么意思字段应该在打字稿中的位置

方括号是什么意思字段应该在打字稿中的位置

本文介绍了方括号是什么意思字段应该在打字稿中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在三个 d.ts 中遇到了这条线:

dispatchEvent(event: { type: string; [attachment: string]: any; }): void;

并想知道这是什么意思.
我知道这意味着一个名为 dispatchEvent 的函数,它接受一个类型为成员类型的参数,但我不确定是什么:

[附件:字符串]:任意;

意思.

解决方案

那是一个索引签名.来自 TypeScript 文档:

可索引类型有一个索引签名,它描述了我们可以用来索引对象的类型,以及索引时相应的返回类型.

因此,例如,您可以为可索引对象定义一个接口,例如:

interface IArrayOfStrings {[索引:数字]:字符串;}

这告诉编译器,对于 IArrayOfStrings 类型的任何对象,通过数字索引访问的任何成员都将是 string 类型.

所以,这将编译没有错误:

interface IArrayOfStrings {[索引:数字]:字符串;}让的话: IArrayOfStrings = ["foo","bar"];让单词:字符串=单词[0];

但这不会:

interface IArrayOfStrings {[索引:数字]:字符串;}让的话: IArrayOfStrings = ["foo","bar"];让 myNumber: number = words[0];

在您的示例中,这一行:

dispatchEvent(event: { type: string; [attachment: string]: any; }): void;

正在描述一种方法 dispatchEvent,它接受一个 { type: string; 类型的参数;[附件:字符串]:任何;}.

为了使该类型更易于理解,请查看定义该类型的接口:

interface IEvent {类型:字符串;[附件:字符串]:任何;}

这告诉编译器 IEvent 类型的对象将有一个名为 type 的字符串属性,以及一个 IEvent 对象的元素,通过访问字符串索引将为 any 类型.

所以,像这样编译不会出错:

interface IEvent {类型:字符串;[附件:字符串]:任何;}让我的事件:IEvent = {类型:'一些事件类型'};让 eventType: string = myEvent["type"];

I came across this line in three.d.ts:

dispatchEvent(event: { type: string; [attachment: string]: any; }): void;

and was wondering what it meant.
I understand that this would mean a function called dispatchEvent which takes an argument of a type with a member type but I am not sure what:

[attachment: string]: any;

means.

解决方案

That is an index signature. From the TypeScript documentation:

So, for example, you could define an interface for an indexable object like:

interface IArrayOfStrings {
    [index: number]: string;
}

This tells the compiler, that for any object of type IArrayOfStrings, any member accessed by the numerical index will be of type string.

So, this will compile without error:

interface IArrayOfStrings {
    [index: number]: string;
}

let words: IArrayOfStrings = ["foo","bar"];

let word: string = words[0];

But this will not:

interface IArrayOfStrings {
    [index: number]: string;
}

let words: IArrayOfStrings = ["foo","bar"];

let myNumber: number = words[0];

In your example, this line:

dispatchEvent(event: { type: string; [attachment: string]: any; }): void;

is describing a method dispatchEvent that accepts one parameter of type { type: string; [attachment: string]: any; }.

To make that type easier to understand, look at an interface that defines this type:

interface IEvent {
    type: string;
    [attachment: string]: any;
}

This tells the compiler that objects of type IEvent will have a string property called type, and elements of an IEvent object, accessed by the string index will be of any type.

So, something like this would compile without error:

interface IEvent {
    type: string;
    [attachment: string]: any;
}

let myEvent: IEvent = {
    type: 'some-event-type'
};

let eventType: string = myEvent["type"];

这篇关于方括号是什么意思字段应该在打字稿中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:36