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.
The brackets declare an index signature, meaning beside type, which is mandatory, you can put anything into the first argument.
Square brackets (also called brackets, especially in American English) are mainly used to enclose words added by someone other than the original writer or speaker, typically in order to clarify the situation: He [the police officer] can't prove they did it.
The [] operator converts the expression inside the square brackets to a string. For instance, if it is a numeric value, JavaScript converts it to a string and then uses that string as the property name, similar to the square bracket notation of objects to access their properties.
Square brackets are used to index (access) elements in arrays and also Strings. Specifically lost[i] will evaluate to the ith item in the array named lost.
When do you use square brackets? In academic writing, you use square brackets to indicate words are added or explained in some way in quoted text, to modify a quote for grammatical reasons, to show missing words with ellipses or to replace expletives. Using square brackets to add words to quoted text
Correct: Leslie Mitchells [of Canada] won the gold medal. The first letter in square brackets typically isn’t capitalized because square brackets usually contain sentence fragments, single words, or single letters/symbols. However, proper nouns are typically capitalized even when used alone in square brackets.
However, square brackets are generally easier to use because they typically do not contain full sentences. Like parentheses, it is considered a grammatical error to use only a single square bracket. The additional information should be entirely contained within two square brackets: Incorrect: Leslie Mitchells of Canada] won the gold medal.
The brackets around the [s] indicate that you altered this slightly so that it would fit in the sentence with “he said.” Ellipses are three dots used to show either a pause in someone’s speech or that words are missing in a sentence.
That is an index signature. From the TypeScript documentation:
Indexable types have an index signature that describes the types we can use to index into the object, along with the corresponding return types when indexing.
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"];
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