-
| ---@param dmginfo CTakeDamageInfo
function foo(dmginfo)
  ---@type Player | Entity
  local attacker = dmginfo:GetAttacker()
  
  # attacker must be player after this!
  if not attacker:IsPlayer() then return end
  # but I still get errors like: Cannot assign `Entity|Player` to parameter `Player`.
  # How can i cast it as such and not Player|Entity
endI want to cast a variable type after declaration for the global scope, my idea is that after i check if the attacker is a certain type i want to cast it as just this type for all the code below | 
Beta Was this translation helpful? Give feedback.
      
      
          Answered by
          
            tomlau10
          
      
      
        Jul 22, 2025 
      
    
    Replies: 2 comments 3 replies
-
| use  | 
Beta Was this translation helpful? Give feedback.
                  
                    3 replies
                  
                
            -
|   If it’s Vscode-EmmyLua, You can use return_cast here to enable automatic inference. | 
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
I believe that you ran into another strange known issue: #2326
where the
@castis not working if you file doesn't have a trailing newline 🙈Could you try to add a new empty newline at the end of your file?