Reputation: 428
I want a define a typealias
for a Dictionary, who's key can only be of type String
and value can only be of type that conform to the Encodable
protocol. The reason for this is so that I can use JSONEncoder
and encode the values later on.
I have tried:
typealias APIParams = [String: T where Value: Encodable]
but it throws an error at me.
How can I accomplish this?
Upvotes: 0
Views: 113
Reputation: 536047
I have tried:
typealias APIParams = [String: T where Value: Encodable]
but it throws an error at me.
Closest to the way you were writing it (with a where clause) would be
typealias APIParams<T> = [String: T] where T: Encodable
Upvotes: 0
Reputation: 275125
If you want to be able to encode a APIParams
later on, you should do:
typealias APIParams<T: Encodable> = [String: T]
Type aliases can be generic! You would now be able to say e.g. APIParams<Int>
, APIParams<String>
, or APIParams<YourCodableStruct>
Upvotes: 1