Key in type does not exist

I have these types in a ModuleScript:

export type Class<T> = {
	new: () -> T
}

export type Component = {
	componentData: ComponentData
}

export type ComponentData = {
	entity: Entity?,

	getEntity: (ComponentData) -> Entity?
}

export type ComponentClass<T> = Class<T> & {
	getComponentName: () -> string
}

export type Entity = {
	components: {[string]: Component},

	addComponent: (ComponentClass<any>) -> nil,

	getComponent: (string) -> Component,

	removeComponent: (string) -> nil
}

export type InventoryComponent = Component & {
	pSize: number,

	pItems: {Item?},

	setSize: (InventoryComponent, number) -> nil
}

export type Item = {
	
}

Elsewhere, I have a function (in a script that requires the above module, also set to --!strict):

function inventorySetSize(self: Types.InventoryComponent, value: number)
	self.pSize = value
end;

On self.pSize, I get a warning:

W000: (16,2) Expected type table, got 't1 where Component = {| componentData: ComponentData |} ; ComponentData = {| entity: nil | {| addComponent: ({| getComponentName: () -&gt; string |} &amp; {| new: () -&gt; any |}) -&gt; nil, ... 3 more ... |}, ... 1 more ... |} ; t1 = Component &amp; {| pItems: {nil | {| |}}, pSize: number, setSize: (t1, number) -&gt; nil |}' instead

Through some debugging attempts, I noticed that replacing self.pSize with a getting statement like print(self.pSize) gives another warning:

W000: (16,8) Type 't1 where Component = {| componentData: ComponentData |} ; ComponentData = {| entity: nil | {| addComponent: ({| getComponentName: () -&gt; string |} &amp; {| new: () -&gt; any |}) -&gt; nil, ... 3 more ... |}, ... 1 more ... |} ; t1 = Component &amp; {| pItems: {nil | {| |}}, pSize: number, setSize: (t1, number) -&gt; nil |}' does not have key 'pSize'

What this second error seems to be saying to me is that the type InventoryComponent does not have the property pSize. My definition of the type does, however.

What is going on here? Does this have to do with the fact that my types have cyclic references? If it is, should I not be doing that, or is it a bug? Or is it something else entirely which Iā€™m just not seeing?

1 Like