Define type of children

I’ve recently started using strict typing and I’d like to know if it’s possible to define the type of the children of an instance

type FolderOfParts = Folder

local function makeBlack(folder: FolderOfParts)
	for i, v in folder:GetChildren() do
		v.Color = Color3.new(0, 0, 0) --will have a warning
	end
end

I tried things like this but they didn’t work

type FolderOfParts = Folder & {Part}
type FolderOfParts = Folder & {[string]: Part}
type FolderOfParts = Folder & {[any]: Part}

I know it’s possible to define the type of specific children but that’s not what I want

type FolderOfSpecificParts = Folder & {
	SpecificPart: Part
}
type FolderOfParts = {BasePart}
-- {} defines a table
-- the type within the curly braces defines whats in it

type FolderOfParts = {[string]: BasePart}
-- defines an index equating to a specific type in a table

--!strict will be pretty strict on how things are phrased or used, so you need to be specific on what you want.

type FolderOfParts = Folder

local function makeBlack(folder: FolderOfParts)
	for i, v:BasePart in folder:GetChildren() do
		v.Color = Color3.new(0, 0, 0) --will have a warning
	end
end	

I think I misunderstood the original post, but hope this helped if this was what you were finding.
( I am not experienced on strict typing )

I can do this:

local function makeBlack(folder: FolderOfParts)
	for i, v in folder:GetChildren() do
		(v :: BasePart).Color = Color3.new(0, 0, 0) --v is asserted as BasePart
	end
end

But it would be nice if there was a way to define the class in the type declaration

Strict typing in lua honestly gives you no benefits

1 Like