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
---@classMyClass---@classMyInheritedClass:MyClass---@fieldtestfun(): void---@returnTypeGuard<MyInheritedClass>functiontypeguard(instance)
returntrueend---@typeMyClasslocalinstance= {}
iftypeguard(instance) theninstance:test() -- this is MyClass still, this is a warning! Undefined field `test`.---@castinstanceMyInheritedClassinstance:test()
end
Tested on the latest update, and also #1017, both have this issue
the same way instanceof would be used in Java. If you have a "wide" class, and you want to access more "specialized" behavior on the child, you want to type check, and then run it.
Is this not a good use of TypeGuard? is there anything else which allows this? It is a very common pattern.
Activity
xuhuanzy commentedon Apr 6, 2026
This behavior is correct:
MyInheritedClassis broader in scope thanMyClass, so it cannot be converted.NyakoFox commentedon Apr 6, 2026
I am not sure I understand. A very desirable thing to do is:
the same way
instanceofwould be used in Java. If you have a "wide" class, and you want to access more "specialized" behavior on the child, you want to type check, and then run it.Is this not a good use of TypeGuard? is there anything else which allows this? It is a very common pattern.