Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts-expect-error before assignment from for expression #1135

Open
bbrk24 opened this issue Apr 2, 2024 · 4 comments
Open

ts-expect-error before assignment from for expression #1135

bbrk24 opened this issue Apr 2, 2024 · 4 comments
Labels
typescript TypeScript compatibility or extensions

Comments

@bbrk24
Copy link
Contributor

bbrk24 commented Apr 2, 2024

In this code, it's impossible to place the @ts-expect-error directive correctly:

declare let t: [number, number, number]

// @ts-expect-error I can't prove this has three elements but it does
t = for i of [1..3]
  Math.sqrt i

The current spelling complains about the type mismatch and about an unused expect-error directive, as it compiles to

declare let t: [number, number, number]

// @ts-expect-error I can't prove this has three elements but it does
const results=[];for (let i1 = 1; i1 <= 3; ++i1) {
  const i = i1;
  results.push(Math.sqrt(i))
};t = results

Current workaround:

t = (for i of [1..3]
  Math.sqrt i
) as any
@edemaine
Copy link
Collaborator

edemaine commented Apr 2, 2024

This seems like the same issue as #1113. Could you move your example to there?

@edemaine edemaine closed this as completed Apr 2, 2024
@bbrk24
Copy link
Contributor Author

bbrk24 commented Apr 2, 2024

I think it's different. That issue can plausibly be fixed by removing a newline from the generated code, and emitting for (const key in obj){const value = obj[key];. This issue requires transposing the comment across the entire body of the for loop, which may be arbitrarily long and isn't necessarily collapsible (e.g. if it contains line comments or a multi-line template string).

@edemaine
Copy link
Collaborator

edemaine commented Apr 2, 2024

Ah, right, sorry.

@edemaine edemaine reopened this Apr 2, 2024
@edemaine
Copy link
Collaborator

edemaine commented Apr 2, 2024

Do you have a proposal for how to fix this? I don't think we can use t in place of results here because t might change in the loop and then semantics change.

One better workaround I think:

sqrts := for i of [1..3]
  Math.sqrt i
// @ts-expect-error I can't prove this has three elements but it does
t = sqrts

It would be nice if this worked, but |> currently can't follow a loop like this:

t = for i of [1..3]
  Math.sqrt i
|> as [number, number, number]

Or perhaps this would be even cleaner, but also doesn't parse as intended:

t = for i of [1..3]
  Math.sqrt i
as [number, number, number]

Back to comments, one thing I would find somewhat natural to write is

t = // @ts-expect-error
  for i of [1..3]
    Math.sqrt i

Unfortunately @ts-expect-error must come on the line before, which seems hard to guarantee...

@edemaine edemaine added the typescript TypeScript compatibility or extensions label Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript TypeScript compatibility or extensions
Projects
None yet
Development

No branches or pull requests

2 participants