Shape and sprite graphics

i am trying to receive in my function as a parameter either a shape or a sprite and then access their graphics property, but this gives me an error:


public static function makeRect( obj:DisplayObject, ...
            
var g:Graphics;
            
if(obj is Sprite){
    g = Sprite(obj).graphics;
}else{
    g = Shape(obj).graphics;
}
                
g.graphics.beginFill(_color, _alpha);
...

//1119: Access of possibly undefined property graphics through a reference with static type flash.display:Graphics.