Can someone help me understand why I’m getting the following behavior from the TypeScript type checker? This seems like a bug to me, but is there something I’m missing here?
Thanks in advance.
- Jonathan
TypeScript version: 2.5.3
Code:
type LinkedListNode<T> =
{
data: T;
next: LinkedListNode<T>;
} |
{ next: null; }
type LinkedList<T> = { head: (LinkedListNode<T> | null); }
const createIterator = <T>(list: LinkedList<T>) => {
let { head: node } = list
return {
[Symbol.iterator]: function* () {
if (node !== null) {
while (node.next !== null) {
yield node.data
node = node.next
}
}
}
}
}
Expected behavior:
No errors.
Actual behavior:
Line 16 of the code above (i.e., the line with yield node.data) generates the following TypeScript error:
Property 'data' does not exist on type 'LinkedListNode<T>'.
Property 'data' does not exist on type '{ next: null; }'.
Can someone help me understand why I’m getting the following behavior from the TypeScript type checker? This seems like a bug to me, but is there something I’m missing here?
Thanks in advance.
- Jonathan
TypeScript version:
2.5.3Code:
Expected behavior:
No errors.
Actual behavior:
Line 16 of the code above (i.e., the line with
yield node.data) generates the following TypeScript error: