rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Improve error message when adding type to generic application

Open nojaf opened this issue 2 months ago • 2 comments

Consider

https://rescript-lang.org/try?version=v12.0.0-beta.13&module=esmodule&code=LYewJgrgNgpgBAdRgIwIIAUCScC8cDeAUHAJCiSxwAiA8gLIbZ5EkkAuAngA7wyzAwAdm2JwAvnEJjChWGzh8YA4QCUYAMwDCIAE46YAY3kAuOGoCGRgHT71AHgBy0KOeSwrbO0jRYrtBr6KymwAfCG4ZjCWbFYQAM4wauoAFE5QLm4wVoLOAJSyMPJBQmxJERbW8YkaXiiMfvT1xcIhqc6u7jnp+UA

module WebAPI = {
 	module DOMAPI = {
		type element
  } 
}

let elementRefCorrect : React.ref<Nullable.t<WebAPI.DOMAPI.element>> = React.useRef(Nullable.null)
let elementRef = React.useRef<WebAPI.DOMAPI.element>(Nullable.null)

In F#, you can specify the React.useRef<WebAPI.DOMAPI.element> syntax. I can live with the fact that ReScript doesn't have the same syntax as F#. But the error message is very confusing:

Type Errors
[E] Line 8, column 30:
The value element can't be found in WebAPI.DOMAPI

The type does exists, whatever I'm going for syntax-wise is the issue here. Could we improve this? Thoughts @zth?

nojaf avatar Oct 17 '25 18:10 nojaf

Interesting! What AST does that parse to?

I'm sure that error can be improved. It's also a common syntax in TS so we should make it as nice as we can.

zth avatar Oct 17 '25 20:10 zth

Two infix operations it seems:

React.useRef<int>(Nullable.null)

gives

expression (A.res[1,0+17]..[1,0+48])
          Pexp_apply
          expression (A.res[1,0+33]..[1,0+34])
            Pexp_ident ">" (A.res[1,0+33]..[1,0+34])
          [
            <arg>
            Nolabel
              expression (A.res[1,0+17]..[1,0+33])
                Pexp_apply
                expression (A.res[1,0+29]..[1,0+30])
                  Pexp_ident "<" (A.res[1,0+29]..[1,0+30])
                [
                  <arg>
                  Nolabel
                    expression (A.res[1,0+17]..[1,0+29])
                      Pexp_ident "React.useRef" (A.res[1,0+17]..[1,0+29])
                  <arg>
                  Nolabel
                    expression (A.res[1,0+30]..[1,0+33])
                      Pexp_ident "int" (A.res[1,0+30]..[1,0+33])
                ]
                transformed_jsx: false
            <arg>
            Nolabel
              expression (A.res[1,0+35]..[1,0+48])
                Pexp_ident "Nullable.null" (A.res[1,0+35]..[1,0+48])
          ]
          transformed_jsx: false

So, this would be something to match during type checking, I suppose?

nojaf avatar Oct 20 '25 08:10 nojaf