I'm not yet familiar with Typescript, and I'm having troubles to define a simple array.
My goal is to have an array where the key is a string and its values are of types Sound.
I have defined an interface like that:
interface SoundsArrayType {
[key: string]: Sound;
}
Then:
class SoundManager {
private sounds: SoundsArrayType;
constructor() {
// error: Type 'undefined[]' is not assignable to type 'SoundsArrayType'.
if(!this.sounds) this.sounds = [];
}
pauseAll() {
for(let sound of this.sounds) {
// error: Type 'SoundsArrayType' must have a '[Symbol.iterator]()' method that returns an iterator.
sound.pause();
}
}
}
I'm not sure how to fix these errors. I read the Interfaces page from the Typescript site but I'm still stuck.
SoundsArrayTypeis not an array but a dictionary-like object. Don't assign[]to it; assign{}instead. Don't try iterating over it directly; if you are using ES2017 or above, you can iterate overObject.values(this.sounds)instead ofthis.sounds.