fable-compiler / Fable

F# to JavaScript, TypeScript, Python, Rust and Dart Compiler
http://fable.io/
MIT License
2.9k stars 295 forks source link

[TS] Make `AsyncBuilder` returns a real type instead of `Any` #3903

Closed MangelMaxime closed 1 day ago

MangelMaxime commented 1 day ago

I was looking at fixing #3864, and it seems like my fix works for the code provided in the issue:

let doAsync =
    async {
        return "hi"
    }

However, it fails on more complex code and I don't understand why.

Argument of type '(ctx: IAsyncContext<number | undefined>) => void' is not assignable to parameter of type 'IAsync<number>'.
  Types of parameters 'ctx' and 'x' are incompatible.
    Type 'IAsyncContext<number>' is not assignable to type 'IAsyncContext<number | undefined>'.
      Type 'number | undefined' is not assignable to type 'number'.
        Type 'undefined' is not assignable to type 'number'.ts(2345)

I don't understand where TypeScript infer the type to XX | undefined we never seems to use it inside of AsyncBuilder.

@ncave Do you have an idea?

ncave commented 1 day ago

Superceded by #3904

P.S. @MangelMaxime Apologies, that was a bit lazy of me, feel free to revert #3904 and resubmit it yourself, if needed.

MangelMaxime commented 13 hours ago

Oh no problem, the solution was so simple...

I was so focused on looking for | undefined that I didn't see the optional argument in the method 🤦‍♂️

And that's probably what caused me to try to restrict where we typed Async in this PR too.

Thank you for taking a look.