The title says it all. This is what I mean:
local table = {}
type table.a = boolean
function getSomething(): table.a
print("got it")
end
If anyone could help, I’d appreciate it! Thanks
The title says it all. This is what I mean:
local table = {}
type table.a = boolean
function getSomething(): table.a
print("got it")
end
If anyone could help, I’d appreciate it! Thanks
type TableType = {
a: boolean,
b: number,
c: string,
}
local tbl: TableType = {
a = true,
b = 5,
c = "hello world"
}
@tlr22 @Content_Corrupted019
I edited the original post to clarify. Your reply isn’t what I’m trying to do
That’s going to require enabling the beta type solver to get the index type function.
-- index<TableType, "a"> resolves to "boolean"
local function getSomething(): index<TableType, "a">
return true
end
Sorry again, let me be more specific
local types = {
a: {
A: number,
B: number
}
}
local table = {
{
A = 1,
B = 2
}:: types.a
}
There’s a lot of incorrect syntax here. Is types
supposed to be a type definition (i.e. prefixed with the type
keyword instead of local
)?
If yes, then I suppose what you’re trying to do is:
type TableType = {
a: {
A: number,
B: number,
}
}
local table = {{A = 5, B = 6} :: index<TableType, "a">}
Why can’t you do the following instead:
type SomeTable = {
A: number,
B: number,
}
local table = {
{
A = 5,
B = 7
} :: SomeTable
}
I’m making a cutscene module
Rough draft of how it would look(doesn’t work)
local actions = {}
export type camera = {
Type: “pan”,
CFrame: CFrame
} | {
Type: “switch”,
CFrame: CFrame
}
export type movePlayer = {
Player: Player,
CFrame: CFrame
}
return actions
How I want it to work from a different script
local action: actions.movePlayer = {
Player = player,
CFrame = CFrame.new()
}
local action2: actions.camera.pan = {
CFrame = CFrame.new()
}
Oh, you want to get type definitions from a module script. All you have to do is require the module, and all exported types should be available.
local Module = require(path.to.module) -- This is your cutscene module
local player: Module.movePlayer = {
Player = player,
CFrame = CFrame.new()
}
Right. But I don’t want to have it unorganized like
type cameraSwitch
type cameraPan
type cameraType
type cameraSubject
Is there a way to just turn it to actions.camera.___?
I also don’t want to have a separate module where it’s like
local cameraActions
local actions
Unless it’s somehow put together like how I said before with actions.camera.___
You can declare your camera type as the following to make the definition shorter, if all your camera types have the CFrame property:
export type CameraType = "switch" | "pan"
export type Camera = {
Type: CameraType,
CFrame: CFrame
}
Then, in another script:
local actions = require(path) -- I'm assuming this is what you'll call your module
local camera: actions.Camera = {
Type = "switch", -- Or any other CameraType
CFrame = CFrame.new()
}
I’m not sure what you mean by this.
But what if a camera action has another property that isn’t included in type Camera?
For example, “type” would need to know the Enum.CameraType?
You might not be able to write the definition as cleanly, then, but I still think this is pretty readable.
export type SimpleCamera = {
Type: "switch" | "pan",
CFrame: CFrame
}
export type SubjectCamera = {
Type: "subject",
CFrame: CFrame,
Subject: Model
}
export type Camera = SimpleCamera | SubjectCamera
-- This will error if there's no "Subject" key
local subjectCamera: Camera = {
Type = "subject",
CFrame = CFrame.new(),
Subject = Instance.new("Model")
}
-- This will error if you add a "Subject" key
local simpleCamera: Camera = {
Type = "switch",
CFrame = CFrame.new(),
}
Thank you for your hard work in trying to help. I’m going with your method for now, but I’m leaving this post open in case anyone finds a better solution.
you can use typeof() to get the type of an element
local table = {}
table.a = true
-- now getSomething returns a bool
function getSomething(): typeof(table.a)
print("got it")
end
and if you changed a to anything else then the type will change automatically
example
local table = {
a = {
b = true,
c = 0,
}
}
-- now getSomething returns a table containing a : true and c : number
function getSomething(): typeof(table.a)
print("got it")
end
To iterate on this, you can actually do this using purely types
--!strict
type TypeA = {A:string, B:number}
local types:{
TypeA:TypeA
} --Notice that we only assigned a type, but never a value.
type test = typeof(types.TypeA.A)--string
local var_a:test = "this is a string" --Valid; matching types
local var_b:typeof(types.TypeA.B) = 2 --Valid; matching types