0

I am trying to destruct an array, but I am getting the following errors:

Type 'undefined' must have a 'Symbol.iterator' method that returns an iterator.

let [alias, config]: [string, DBConfig] | undefined = Object.entries(info).find(...)

This is what the find definition shows, it is the same as what I defined.

(method) Array<[string, DBConfig]>.find(predicate: (
  value: [string, DBConfig], 
  index: number, 
  obj: [string, DBConfig][]) => boolean, thisArg?: any
): [string, DBConfig] | undefined (+1 overload)

If I remove my definition so it looks like this:

let [alias, config] = Object.entries(info).find(...)

I then get the same error. What can I do (without using any) to get this working?

1 Answer 1

1

The whole problem comes down to the fact that you're trying to use destructuring on something that might not be destrcturable (e.g. undefined).

If you are sure let [alias, config] = Object.entries(info).find(...) will never result in undefined in your use cases then you just have to cast it :

let [alias, config] = Object.entries(info).find(/*...*/) as [string | DBConfig];

If it may result in undefined then you will have to accept a nullable DBConfig

let [alias, config] = Object.entries(info).find(/*...*/) || ["", null];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.