Computed interface values in TypeScript?

Viewed 43

So basically, I was looking around discordjs's typings folder and found this:

export interface ClientEvents {
  ...
  messageCreate: [message: Message] <- what?
  messageDelete: [message: Message | PartialMessage]
}

What exactly are they trying to do here? I've heard of index signatures, like in this example here:

interface Foo {
  [bar: string]: any
}

But I've never seen any syntax that resembles this in both TS and JS.

1 Answers

Credits to jonrsharpe's comment under the question. The syntax mentioned was a variation of a tuple in TypeScript 4.0 which includes labels:

Regular tuple:

// has no indication of parameter names
type Range = [number, number] 

Labelled tuple:

type Range = [start: number, end: number] 
// spreading this labelled tuple in a function's parameters...
function getRange(...range: Range) 

// ... results in the following at compile time:
function getRange(start: number, end: number)

Very cool!

Related