DanielXMoore / Civet

A TypeScript superset that favors more types and less typing
https://civet.dev
MIT License
1.36k stars 29 forks source link

`break with` and `continue with` for modifying results array in a loop #1396

Closed edemaine closed 2 weeks ago

edemaine commented 2 weeks ago

Fixes #1395

TODO: Documentation

An issue with TypeScript is clear in the loop example: we have let results = [] but later results = 'done'. Perhaps we could detect specifically a loop that has no plain break and thus will only be exited via break with, and then we could just write let results.

edemaine commented 2 weeks ago

One solution to help TypeScript is to declare the results as let results;results=[]. This gives it the proper union type of all possible exits from the loop.

Good idea! Implemented, and it seems to work well.

I also removed results=[] in the special case of loop containing break with but not break.