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

export interface Football{
  goalPost:any
  ball:any
}

export interface TableTennis{
  bat:any
  ball:any
}
kelsny
  • 23,009
  • 3
  • 19
  • 48
Sayan
  • 47
  • 12

2 Answers2

0

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

LeoDog896
  • 3,472
  • 1
  • 15
  • 40
  • Suppose this team interface is the response model of an api. Let's say teamResponse. How to access value of the key called table-tennis from teamResponse. Because teamResponse.table-tennis gives error – Sayan Nov 10 '22 at 02:40
  • You use teamResponse["table-tennis"] – LeoDog896 Nov 10 '22 at 12:15
  • I declared a const tableTennis="table-tennis" and that solved my problem. I was able to access the bat using teamResponse.tableTennis.bat – Sayan Nov 10 '22 at 17:19
0

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

export interface team {
    football: Football;
    "table-tennis": TableTennis;
}
kelsny
  • 23,009
  • 3
  • 19
  • 48
  • But in that case how would I be able to access it from the response object. – Sayan Nov 10 '22 at 02:36
  • @Sayan `response["table-tennis"]`? – kelsny Nov 10 '22 at 02:46
  • Suppose this team interface is the response model of an api. Let's say teamResponse. How to access value of the key called table-tennis from teamResponse. Because teamResponse.table-tennis gives error. Incase of football we can access with teamResponse.football – Sayan Nov 10 '22 at 03:07
  • You use teamResponse["table-tennis"] – LeoDog896 Nov 10 '22 at 12:15
  • In that case how I can I access bat or ball which is inside table-tennis interface. Like teamResponse["table-tennis"].bat doesn't work – Sayan Nov 10 '22 at 15:28
  • See https://javascript.info/object and [What is a help vampire?](https://meta.stackoverflow.com/questions/258206/what-is-a-help-vampire) – kelsny Nov 10 '22 at 15:29