I have a basic linked list build in typescript with a discriminated union.
type ListType<T> = {
Kind: "Cons",
Head: T,
Tail: List<T>
} | {
Kind: "Empty"
}
type ListOperations<T> = {
reduce: <U>(this: List<T>, f: (state: U, x: T) => U, accumulator: U) => U
map: <U>(this: List<T>, f: (_: T) => U) => List<U>
reverse: (this: List<T>) => List<T>
concat: (this: List<T>, l: List<T>) => List<T>
toArray: (this: List<T>) => T[]
join: (this: List<List<T>>) => List<T>
}
type List<T> = ListType<T> & ListOperations<T>
I also have some constructors for both empty and cons:
export const Cons = <T>(head: T, tail: List<T>): List<T> => ({
Kind: "Cons",
Head: head,
Tail: tail,
...ListOperations()
})
export const Empty = <T>(): List<T> => ({
Kind: "Empty",
...ListOperations()
})
And finally I have an implementation of the different methods:
const ListOperations = <T>(): ListOperations<T> => ({
reduce: function <U>(this: List<T>, f: (state: U, x: T) => U, accumulator: U): U {
return this.Kind == "Empty" ? accumulator : this.Tail.reduce(f, f(accumulator, this.Head))
},
map: function <U>(this: List<T>, f: (_: T) => U): List<U> {
return this.reduce((s, x) => Cons(f(x), s), Empty())
},
reverse: function (this: List<T>): List<T> {
return this.reduce((s, x) => Cons(x, s), Empty())
},
concat: function (this: List<T>, l: List<T>): List<T> {
return this.reverse().reduce((s, x) => Cons(x, s), l)
},
toArray: function (this: List<T>): T[] {
return this.reduce<T[]>((s, x) => s.concat([x]), [])
},
join: function (this: List<List<T>>): List<T> {
return this.reduce((s, x) => s.concat(x), Empty())
}
})
Everything works fine, but I get a compile error when I try to run the following:
let x = Cons(1, Cons(2, Cons(3, Cons(4, Empty()))))
let y = x.map(x => x + 4)
let z = Cons(x, Cons(y, Empty()))
z.join()
The 'this' context of type
List<List<number>>
is not assignable to method's 'this' of typeList<List<List<number>>>
.
This is because of the join
method (or flatten
as some of you might call it). When I write the join outside the List type then it works, so my question is: Is there a way to explicitly tell the compiler that this
needs to be of type List<List<T>>
?
I already tried using extends
join: function <T1 extends List<T>>(this: List<T1>): List<T>
That is because your list is a List<T>
, whereas T
is itself a List<T>
. The correct typing would be:
join(this: List<T>): T {
To then ensure that T
is a List itself use a conditional type:
join(this: T extends List<*> ? List<T> : "Only nested lists can be joined!"): T