Eduardo
Eduardo

Reputation: 1831

Typescript: Defining an Object

I am trying to define an object:

errors = {}

Then I want to set some items like:

errors['a'] = 'aaaa';
errors['b'] = 'bbbb';
errors['c'] = 'cccc';

But I get errors saying that property 'a' (or any of the other) are undefined. For solving that I add those items to the initial value:

errors = {
'a': '',
'b': '',
'c': ''
}

I have 2 questions:

  1. What is the type I have to assign to errors?
  2. How can I define an object with dynamic properties?

Upvotes: 2

Views: 88

Answers (1)

unional
unional

Reputation: 15619

What is the type I have to assign to errors?

let errors: { a: string, b: string, c: string }

How can I define an object with dynamic properties?

let errors: Record<string, string>

Upvotes: 2

Related Questions