stc icon indicating copy to clipboard operation
stc copied to clipboard

Use `Iterable` instead of `Iterator` for generator functions

Open kdy1 opened this issue 3 years ago • 5 comments

https://github.com/dudykr/stc/blob/e69d14c1b30473f73bc8873400e950d44a047e64/crates/stc_ts_file_analyzer/tests/tsc/types/builtin/generator/bad/.1.ts#L1-L2

This should fail because a generator function uses the item from Iterable as yield type. i.e., g3 is inferred to emit a string as an item, and the inferred generator (Generator<number, any, undefined>) is assigned to BadGenerator.

It should file with

tests/tsc/types/builtin/generator/bad/1.ts:2:24 - error TS2322: Type 'Generator<string, any, undefined>' is not assignable to type 'BadGenerator'.
  The types returned by 'next(...)' are incompatible between these types.
    Type 'IteratorResult<string, any>' is not assignable to type 'IteratorResult<number, any>'.
      Type 'IteratorYieldResult<string>' is not assignable to type 'IteratorResult<number, any>'.
        Type 'IteratorYieldResult<string>' is not assignable to type 'IteratorYieldResult<number>'.
          Type 'string' is not assignable to type 'number'.

But currently, stc fails to emit an error because stc uses the Iterator interface instead of the Iterable interface while inferring the yield type.

https://github.com/dudykr/stc/blob/e69d14c1b30473f73bc8873400e950d44a047e64/crates/stc_ts_file_analyzer/src/analyzer/stmt/return_type.rs#L176-L181

kdy1 avatar Nov 03 '22 10:11 kdy1

Hi, I want to take this issue🙋‍♂️ @kdy1

timeTravelCYN avatar Nov 03 '22 12:11 timeTravelCYN

Cool, thanks!

kdy1 avatar Nov 03 '22 12:11 kdy1

@timeTravelCYN Are you working on it? Feel free to tell me if you want me to take it (or simply unassign)

kdy1 avatar Nov 11 '22 02:11 kdy1

Do we have to implement get_iterable_element_type from scratch including all functions related with iterable like get_next_value_type_of_iteratable?

devgony avatar Nov 15 '22 12:11 devgony

Not sure, but I think we can reuse many parts of it. Maybe we can extract some common logic to a function

kdy1 avatar Nov 15 '22 12:11 kdy1

Closing as this is already fixed

kdy1 avatar Mar 23 '23 12:03 kdy1