luau icon indicating copy to clipboard operation
luau copied to clipboard

Typechecking says keys don't exist but still shows the keys?

Open HarmosCreations opened this issue 10 months ago • 4 comments

--!strict
local super = require(script.Parent.Parent.Parent.Classes)

local public = {}

function public.Sprint(self:Class) -- even tried with :
	
	self.test = 2  -- Type 'Class' does not have key 'test' or  Cannot add property 'test' to table 'Class'
	
end

local module = {}

setmetatable(module, {__index = super})

local log = module.log

export type Class = typeof(module.new(workspace))

function module.new(character:Model)
	
	local meta = {__index = public}
	local this = setmetatable({}, meta)
	
	this.Humanoid = character:FindFirstChild("Humanoid") :: Humanoid
	
	this.Settings = {}
	
	
	if (not this.Humanoid) then error(character.Name.." is dead!") end
	
	this = setmetatable(this, {__index = super.new()})

	--TODO
	
	return this
end

return module

am i doing something wrong because even the typechecker shows me that test is a property of type Class why when i try to access it, it tells me otherwise?

and can you guys fix self so when we make a function with : we can still identify self inside the parameters instead of self:unknown.

HarmosCreations avatar Feb 11 '25 16:02 HarmosCreations

and can you guys fix self so when we make a function with : we can still identify self inside the parameters instead of self:unknown.

There is an outstanding RFC that we accepted and will implement that improves the type inference behavior around self types. This should likely make what you want to happen automatically, but if you specifically want the language to support annotating the self type in some way when doing a : definition, then that would need to be designed and proposed as its own RFC in luau-lang/rfcs, and I am unsure as to what syntax would actually be viable towards that end.

aatxe avatar Feb 11 '25 18:02 aatxe

and can you guys fix self so when we make a function with : we can still identify self inside the parameters instead of self:unknown.

There is an outstanding RFC that we accepted and will implement that improves the type inference behavior around self types. This should likely make what you want to happen automatically, but if you specifically want the language to support annotating the self type in some way when doing a : definition, then that would need to be designed and proposed as its own RFC in luau-lang/rfcs, and I am unsure as to what syntax would actually be viable towards that end.

I tried the first option and it's worse then my system it has more issues than anything i have created very very unreliable. and even directly doing module.__index = module still does not automatically assign self variables.

HarmosCreations avatar Feb 11 '25 23:02 HarmosCreations

You could not possibly have tried the first option because we have not implemented shared self type inference. It is a future addition the language that we will develop to improve this situation, not something you can already use today.

aatxe avatar Feb 11 '25 23:02 aatxe

You could not possibly have tried the first option because we have not implemented shared self type inference. It is a future addition the language that we will develop to improve this situation, not something you can already use today.

great im looking forward to it.

HarmosCreations avatar Feb 12 '25 12:02 HarmosCreations