#Help with type-checking metatables.

1 messages · Page 1 of 1 (latest)

solemn solar
#
type MyType = {[string]: number}
local Module = {
  "Hello" = 42
}

setmetatable(Module, {})

return Module :: MyType

Is there any way to make this cast work without removing it entirely?

eager ginkgo
#

One way is you first cast it to any then your type

The second way is you use typeof to make your type a metatable as well

#
Return (module :: any)  :: MyType
#

Or

#
type MyType = typeof(setmetatable({} :: {[string]: number}, {})) 
#

Or
Another way is to just use --!strict mode and let Roblox workout the type automatically

solemn solar
#

All hail Suphi 🙏

solemn solar
#

My example actually has the linter giving me a warning about incompatible types

#
return (Module :: unknown) :: MyType

unknown also works if for some reason, you have an irrational fear of the word 'any'