microsoft / microsoft/TypeScript

Bug in typing iterable iterator with symbol

Open
#32,408 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Literal Types
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.6.0-dev.20190713

Search Terms: symbol iterableiterator

Code

const Foo = Symbol()
type FooType = typeof Foo

type SomeType = number | FooType

function giveMeSomeType(): SomeType {
    return (Math.random() > 0.5) ? 42 : Foo
}

const it: IterableIterator<SomeType> = [giveMeSomeType()][Symbol.iterator]()

Expected behavior: local constant it is typechecked correctly

Actual behavior:

Typechecking fails with error:

index.ts:10:7 - error TS2322: Type 'IterableIterator<number | symbol>' is not assignable to type 'IterableIterator<SomeType>'.
  Type 'number | symbol' is not assignable to type 'SomeType'.
    Type 'symbol' is not assignable to type 'SomeType'.

10 const it: IterableIterator<SomeType> = [giveMeSomeType()][Symbol.iterator]()

Playground Link: http://www.typescriptlang.org/play/#code/MYewdgzgLgBAYiEMC8MDKBPAtgIxAGwAoBKAKCgwAcBTeRAFSttQppADM6RTyn0Qs1RjRQwwAV1zUATjAA+XYdR4B6FTA4xQ46RFoBDCBEm0oAC32xzASwgx24sMCjXwWkOPwATGNOpQdMBhqa3MZLg1ZCSlpUgcnFzcAc2sAN2oAWWo0ASEmEgAufkElGABvUhgq339AmAAWACZSAF8eUEhYUKKASSgZfRx8aj6BqBBpAB4ckqYAPlEAbRT0rJm8mhIAXUXMXAIAOlCxia2SUiA

Related Issues: couldn't find

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example from index.ts in the linked Playground and confirm the reported assignability error for IterableIterator. Trace how iterable iterator types handle the number | symbol union, then verify that the example typechecks without the TS2322 error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.