Type Error: Type unknown could not be converted into number

Hi DevForum users!

So while scripting, i changed the code to be strict, but when trying to specify types, for some reasson, some parts of the code have this weird thing.

This is how i define the types:

export type Lerp = {
	Completed : any,
	Stopped   : any,
	Deleted   : any,
	Object    : BasePart,
	Starting  : CFrame,
	DTime     : number,
	Loop      : number,
	Time      : number,
	Playing   : boolean,
	Reverse   : boolean,
	Id        : number,
	Alpha     : number,
	Value     : CFrame,
	Info      : LerpInfo
}

export type LerpService = typeof(setmetatable({}::{
	create : () -> Lerp
}, {}::{
	__index       : typeof(getmetatable(   ({}::Lerp)  )),    -- returns CarControls
}))

And here is how i construct the new Lerp (which is my class)

function LerpService.create(Object: any, Property: {[any]: CFrame}, LerpInfo: LerpInfo): Lerp
	local Name: any, Value: any = GetTableValues(Property)

	local Lerp = setmetatable({}:: Lerp,LerpService)
	Lerp.Completed = GoodSignal.new()
	Lerp.Stopped   = GoodSignal.new()
	Lerp.Deleted   = GoodSignal.new()
	Lerp.Object    = Object 			:: BasePart
	Lerp.Starting  = Object.CFrame
	Lerp.DTime     = 0
	Lerp.Loop      = 0
	Lerp.Time      = 0
	Lerp.Playing   = false
	Lerp.Reverse   = false
	Lerp.Id        = #Lerps + 1
	Lerp.Alpha     = 0
	Lerp.Value     = Value              :: any
	Lerp.Info      = LerpInfo or LerpService.NewLerpInfo(1,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0) :: LerpInfo
	CreationWarnings(Lerp, Lerp.Info)

	Debug.print("Lerp ("..Lerp.Id..") created:",Lerp)
	return Lerp
end

One extra note is that i aswell would like to fix another type thing, which is that it dosent autocomplete outside the module, and in the LerpService modules, the functions appear, but those functions should be for the lerp not the service.


It’s a type error meaing it doesn’t do anything. If you want to get rid of it you could set the luau type check mode to nonstrict in workspace properties

1 Like

Yeah but the point is to make it strict.

The type error isn’t gonna affect your code at all in anyway so this post seems very unnecessary to me

1 Like

If it does or not affect it would not really like having that in my code, plus, i asked for help to add the autocomplete outside the module aswell.

Asking questions about working with the type system is valid and shouldn’t be discouraged. The type system is useful and using it properly is extremely helpful.

I couldn’t get any such error trying to reproduce this myself with the new type solver—try enabling that beta and see if it helps.

It’s important to state that your type structure is in very poor style. Lerp is never defined to have any functions attached to it, which is why you don’t see completion helpers. LerpService should not be the metatable of Lerp. Lerp should really just have its own, private metatable.

The LerpService type shouldn’t exist. It’s bad style to explicitly give your module a type as you remove all of the automatic type information, and you shouldn’t be using it like a metatable.

3 Likes

And how would i convert it to be its own meta table but still be associated with lerp service?

I don’t see why you can’t just have it as its own thing.

local lerpMeta = {}

function lerpMeta:Play() end

export type Lerp = typeof(setmetatable({ --[[fields]] }, lerpMeta))

local LerpService = {}

function LerpService.create(): Lerp
	local Lerp: Lerp = setmetatable({}, lerpMeta)

	return Lerp
end

return LerpService
1 Like

So did it like this:

Type:

local LerpMeta    = {}
export type Lerp = typeof(setmetatable({ }::{
	Completed : any,
	Stopped   : any,
	Deleted   : any,
	Object    : BasePart,
	Starting  : CFrame,
	DTime     : number,
	Loop      : number,
	Time      : number,
	Playing   : boolean,
	Reverse   : boolean,
	Id        : number,
	Alpha     : number,
	Value     : CFrame,
	Info      : Type.LerpInfo,
	Play      : () -> ()	
}
, LerpMeta))

Constructor:

function LerpService.create(Object: BasePart, Property: {[any]: CFrame}, LerpInfo: Type.LerpInfo): Lerp
	local Name: any, Value: any = Utils.GetTableValues(Property)

	local LerpObject = setmetatable({},LerpMeta)
	LerpObject.Completed = GoodSignal.new()
	LerpObject.Stopped   = GoodSignal.new()
	LerpObject.Deleted   = GoodSignal.new()
	LerpObject.Object    = Object 			:: BasePart
	LerpObject.Starting  = Object.CFrame
	LerpObject.DTime     = 0
	LerpObject.Loop      = 0 :: number
	LerpObject.Time      = 0
	LerpObject.Playing   = false
	LerpObject.Reverse   = false
	LerpObject.Id        = #Lerps + 1
	LerpObject.Alpha     = 0
	LerpObject.Value     = Value              :: any
	LerpObject.Info      = LerpInfo or LerpService.NewLerpInfo(1,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0) :: Type.LerpInfo
	Utils.CreationWarnings(LerpObject:: {[any]: any}, LerpObject.Info:: Type.LerpInfo)

	Debug.print("Lerp ("..LerpObject.Id..") created:",LerpObject)
	return LerpObject :: Lerp
end

And i separated the meta tables and to test, added a function to the table, now, how would i do the autocomplete work?
This is my first time trying to use types.

I made it work!

So i had a wrong understanding of how Metatables work, after watching some youtube videos on OOP and stuff, i have now fixed.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.