Skip to content

Strange type checker bug … #19490

@jonathanmarvens

Description

@jonathanmarvens

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; }'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions