In a (theoretical) module, I have this code declaring an array type:
export type Array<T> = {[number]: T}
How do I then use this type in another script?
In a (theoretical) module, I have this code declaring an array type:
export type Array<T> = {[number]: T}
How do I then use this type in another script?
So far i know export is not a thing in Roblox lua.
I don’t think (EDIT: See my next response)export
works yet. It’s a little annoying.
The values they return at least keep their types but you can’t use them:
ModuleScript:
--!strict
export type Point = {x: number, y: number} -- export doesn't change anything
local p: Point = {x = 1, y = 1}
return p
Script:
--!strict
local p = require(script.ModuleScript)
-- it type checks and even knows the name of the type, as expected:
print(p.ASDF) -- error: Key 'ASDF' not found in table 'Point'
-- ...but you can't use it yourself for some reason (with or without export)
local myP: Point = {x = 3, y = 4} -- error: Unknown type 'Point'
BTW @ILiekMelons there’s a new array syntax now—{string}
is the same as {[number]: string}
. So maybe that fixes your specific problem.
I was completely wrong, my bad!
Right after I wrote that I found this: Type checking - Luau
I’ve never seen this part of the docs before
But you can do it like this:
Module:
--!strict
export type Point = {x: number, y: number}
return nil -- just so you can require() this without it complaining
Script:
--!strict
-- Module is nil but we can still use its types
local Module = require(game.ReplicatedStorage.Point)
-- local p: Module.Point = 4 -- error: type 'number' cannot be converted into 'Point' - woohoo!
local p: Module.Point = {x = 1, y = 2} -- okay!
I didn’t see that either, maybe they just added it or something. Thanks!