You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you call setmetatable with an intersection of two or more tables, the resulting type will not have the metatable applied to it. This causes issues with typechecking, as attempting to access something in the __index will cause a type error. I have wrote the following sample code, which mimics a typical OOP setup you might see:
--!strictlocalIndex= {}
localMetatable= {
__index=Index,
}
functionIndex.GetName(self: Foo): stringreturnself.Nameendtypeprops1= {
Name: string,
}
typeprops2= {
Value: any,
}
typefooProps=props1 & props2-- If props2 were removed from this line, there would be no issues-- The type does not include the metatabletypeFoo=typeof(setmetatable({} :: fooProps, Metatable)) -- type Foo = { Name: string } & { Value: any }-- Trying to set an object with the same metatable to be the type Foo doesn't worklocaltestFoo: Foo=setmetatable({
Name="Test",
Value=1,
}, Metatable) -- TypeError: Type '{ @metatable Metatable, { Name: string, Value: number } }' could not be converted into 'props1 & props2'-- Since the type Foo doesn't have an __index, functions in Index are not usabletestFoo:GetName() -- TypeError: Type 'props1 & props2' does not have key 'GetName'
This occurs in both strict and nonstrict mode.
I ran into this issue myself, as I have 2 objects with certain shared properties between them. To simplify the code and allow for easier modifications in the future, I want to store the shared properties in one type and make each object intersect it with a type containing the unique properties for that object before setting the metatable. Unfortunately, this bug makes that idea impossible.
The text was updated successfully, but these errors were encountered:
If you call setmetatable with an intersection of two or more tables, the resulting type will not have the metatable applied to it. This causes issues with typechecking, as attempting to access something in the __index will cause a type error. I have wrote the following sample code, which mimics a typical OOP setup you might see:
This occurs in both strict and nonstrict mode.
I ran into this issue myself, as I have 2 objects with certain shared properties between them. To simplify the code and allow for easier modifications in the future, I want to store the shared properties in one type and make each object intersect it with a type containing the unique properties for that object before setting the metatable. Unfortunately, this bug makes that idea impossible.
The text was updated successfully, but these errors were encountered: