Is there a difference in these type casting?

local text: {string} = generator.generateIpsum(100)
local text = generator.generateIpsum(100) :: {string}

Your first example sets the permanent type of text
Your second example lets the type of text remain flexible, but temporarily clarifies a type

Could you give more explanation on this?

I’ll me happy to mark you as the solution if you do so.

I will be your 200th solution :innocent:

The latter is called a “type cast”. This allows you to manually refine or alter an adjacent type.

A variable is formally typed by suffixing its identifier with : type. By doing this, you declare that the variable will hold no other type. If you do not make this declaration, then the variable will adopt the types it is implicitly given throughout its lifetime, such as return types from functions or type casts. If the variable adopts more than one type, it builds a type union.

Use the first example if you want your variable to only accept a single type of data. Use the second example if you just want to leverage IntelliSense

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.