Struct with generic as SwiftUI binding

Hello. I'm trying to use a struct that accepts a generic as a piece of state in SwiftUI but I'd like the generic to be able to take in multiple different data types. I need the types to be different since they are Codable and need to be encoded into JSON as part of the program.

I tried creating a protocol that contains the shared property needed to render the view, however it's not working. Swift requires that I specify an explicit type for the generic.

struct Base<T>: Codable, Equatable where T: Codable & Equatable {
    var propA: Int
    var propB: String
    var value: T
}

protocol Shared: Codable, Equatable {
    var sharedProperty: String { get set }
}

struct Parent1: Shared, Codable, Equatable {
    var sharedProperty: String
    var parent1Prop: String
    ...
}

struct Parent2: Shared, Codable, Equatable {
    var sharedProperty: String
    var parent2Prop: String
    ...
}

// In SwiftUI view

// I want this to be able to be either Parent1 or Parent2
@Binding var foo: [Base<???>]

// For rendering the view, we only use the shared property
ForEach(foo) { element in
    Text(element.sharedProperty)
}

// The other struct properties are needed to encode the data later on

Any help is greatly appreciated!

Perhaps some sort of type erasure is inevitable here (but see below). Not sure what way is the best, here's one possible implementation:

protocol Shared {
    var sharedProperty: String { get set }
}

struct Parent1: Shared, Codable, Hashable  {
    var sharedProperty: String
    var parent1Prop: String
}

struct Parent2: Shared, Codable, Hashable {
    var sharedProperty: String
    var parent2Prop: String
}

struct TestView: View {
    @Binding var foo: [AnyHashable]
    
    var body: some View {
        List {
            ForEach(foo, id: \.self) { element in
                let shared = element as! Shared
                Text(shared.sharedProperty)
            }
        }
    }
}

let parent1 = Parent1(sharedProperty: "hello", parent1Prop: "world")
let parent2 = Parent1(sharedProperty: "goodbye", parent1Prop: "earth")

struct ContentView: View {
    @State var parents = [parent1, parent2].map { $0 as AnyHashable }
    
    var body: some View {
        TestView(foo: $parents)
    }
}

Or make your item a common data type - an enum with appropriate associated values.

Personally, I'd not try to outsmart it. SwiftUI wants some simple data structures, don't try to be too clever to compress, say, two pages of swift code into one page. Something as simple as a "switch" by all possible item types (how many of those do you plan to have, realistically, perhaps less than a dozen) is what I'd do.