Home > Software design >  How to write a key name in the type script interface if it has '-'
How to write a key name in the type script interface if it has '-'

Time:11-10

export interface team {
  football:Football
  table-tennis:TableTennis
}

export interface Football{
  goalPost:any
  ball:any
}

export interface TableTennis{
  bat:any
  ball:any
}

CodePudding user response:

Encase the key name in quotes, similar to how you would do it in JSON

export interface team {
    football: Football,
    "table-tennis": TableTennis
    // use a string!
}

export interface Football {
    goalPost: any,
    ball: any
}

export interface TableTennis {
    bat: any,
    ball: any
}

TS Playground

CodePudding user response:

You would use quotes, like how you would in JavaScript:

export interface team {
    football: Football;
    "table-tennis": TableTennis;
}
  • Related