Typescript for propert value of object of indefinite number of parameters

Viewed 28

Let's say we have the variable

var Definitions = {
    "eat": "put (food) into the mouth and chew and swallow it",
    "drink": "take (a liquid) into the mouth and swallow.",
    "fly": "move through the air using wings."
}

I need to add type annotation for the Definitions variable to make sure each value is a string.

I tried things like:

var Definitions: {...string[]} = {
    "eat": "put (food) into the mouth and chew and swallow it",
    "drink": "take (a liquid) into the mouth and swallow.",
    "fly": "move through the air using wings."
}

and

var Definitions: {words:...string[]} = {
    "eat": "put (food) into the mouth and chew and swallow it",
    "drink": "take (a liquid) into the mouth and swallow.",
    "fly": "move through the air using wings."
}

But no luck

1 Answers

Try this:

var Definitions: {
  [key: string]: string
} = {
    "eat": "put (food) into the mouth and chew and swallow it",
    "drink": "take (a liquid) into the mouth and swallow.",
    "fly": "move through the air using wings."
}
Related