I’m used to vanilla JavaScript but not TypeScript. I had need to do the following:
let tileMap = {
width: 32,
height: 32,
tiles:
}
I would then store the tiles of a tilemap in that array. However TypeScript does not let me use an array with no type definition. The closest I could get to making this work was by using an interface.
interface tileMapSave {
name : "",
width: 32,
height: 32,
tiles: Image[]
}
let tileMap: tileMapSave
But I’m left wondering if this is the appropriate use of an interface and if there is a different typescript way of creating the object I want. Any clarification would be useful as I’m feeling rather fuzzy-headed about this. Is an interface just for forcing me to make random objects I make have a “type”?
What are you trying to store, exactly? Are you trying to implement your own version of the built in tilemap functionality? Are you trying to store the image of each tile? The type of tile? The location? Or are you trying to store a combination of these?
Essentially, yes. This is the “TypeScript way” of doing things. Normally you can omit the type but since you have an array in your object it requires a generic type. And since you can’t add a type for only one property (for some reason) you have to type out the whole object, for which the easiest method is an interface.
In my game the player can change the tiles of a tilemap. I want to be able to save these alterations between scenes. Im sure there are other ways to do it - the specific implementation isnt so important right now, I’m just trying to understand TypeScript.