Generics are about keeping the type checks at compile time, where they can do the most good.
The result of typeof() and the if only resolves at runtime. So even if it makes sense for us humans that it could only be that one case, the compiler can not be sure of it. Only if the comparison was something like if(true)
or if(a == true)
, could the compiler correctly infer what will happen. It needs compile time constants and literals to be sure of anything.
Also the whole concept of putting type specific code there is anathema to the purpose of generics. Whatever problem you try to solve here, is likely a XY problem. So you should be going a few steps back and explain what the X is, so we can tell you what the proper alternative to this Y is.