How to type cast array?

Just wondering. I’ve checked the luau docs, experimenting, and googling. I found nothing. I tried this though:

local array: {[1]: {}, [2]: number} = {{}, 0}

thanks!

Can you please elaborate on what you want to achieve?

2 Likes

There is no casting operation because table elements do not have types.

1 Like

Yeah, more info would be great.

I would like it so arrays can be type casted. The piece of code above will result in a type error.

The types not matching have no effect on execution. The warnings only exist in the editor and are there for guidance.

I’ve found you can type an entire array, not specific index’s im pretty sure. I’ve noticed a huge perfomance speed up on array modification if you give it a type before hand.

I’m pretty sure the way I did it was

local arrayExample : {OBJECT_TYPE} = {}

1 Like

You can assign types for dictionary values
e.g,

type dict = {
	Fruit: string,
	Amount: number,
}

and you can assign types for a whole array:
e.g.

type array = {number}

However, you can’t have multiple types in an array.

2 Likes