Fix caching logic in baseTypeRef

Typevars can be parts of larger types or underlying types of
other types, which renders these other types uncacheable. The new
logic takes account of that.