Add CommonUnderlying function to report a type's shared underlying type
Tools built on go/types often need the single underlying type shared by every type in a type parameter's type set. This concept was called the core type. go/types computes it internally but does not export it, so x/tools and other projects have each reimplemented the logic.
The proposal adds an exported function, CommonUnderlying, that returns the common underlying type of a type. For an ordinary type, the result is its underlying type. For a type parameter, the result is the underlying type shared by all types in its type set. If no such type exists, the function returns nil.
// T is a type parameter constrained by ~[]int | MySlice
if u := types.CommonUnderlying(T); u != nil {
if s, ok := u.(*types.Slice); ok {
fmt.Println("element type:", s.Elem())
}
}
- Proposal
- #60994 by nevkontakte
- Status
- Implemented on Nov 18, 2026
- Release
- Go 1.28
- Impact
- low
- Area
- go/types