In Swift there is such concept as associated types.
protocol Container {
associatedtype ItemType // Here it is.
mutating func append(item: ItemType)
var count: Int { get }
subscript(i: Int) -> ItemType { get }
}
struct IntStack: Container {
typealias ItemType = Int // Here again.
mutating func append(item: Int) {
self.push(item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> Int {
return items[i]
}
var items = [Int]()
mutating func push(item: Int) {
items.append(item)
}
mutating func pop() -> Int {
return items.removeLast()
}
}
It's a kind of generic interfaces, but one important feature of associated types is that they can be refered from outside of the containing type.
var x: IntStack.ItemType = someIntStack.pop()
Is it possible to make somethig like this in TypeScript?