solid-refresh icon indicating copy to clipboard operation
solid-refresh copied to clipboard

getForeignBindings doesn't understand enum imports

Open tmm1 opened this issue 10 months ago • 2 comments

i have a tsx file roughly like:

import { ResultKind } from './kind.js'
...
resolvedCommand.kind === ResultKind.FOO

where kind.js is:

export const enum ResultKind {
	FOO,
	BAR,
}

the tsx gets translated to:

// no import of ResultKind in generated js
...
resolvedCommand.kind === 2 /* ResultKind.FOO */
...
    location: "App.tsx:186:7",
    signature: "b58d5153",
    dependencies: () => ({
...
        ResultKind,
        setTimeout,

resulting in an error:

ERR ResultKind is not defined: ReferenceError: ResultKind is not defined
    at Object.dependencies (http://localhost:5173/App.js?t=1736753306453:2814:9)
    at patchComponent (http://localhost:5173/node_modules/.vite/deps/solid-refresh.js?v=81f91454:108:89)
    at patchComponents (http://localhost:5173/node_modules/.vite/deps/solid-refresh.js?v=81f91454:130:9)
    at patchRegistry (http://localhost:5173/node_modules/.vite/deps/solid-refresh.js?v=81f91454:167:40)

tmm1 avatar Jan 13 '25 08:01 tmm1

This might be a no-fix (but I'm not closing this because this is a good issue) for the following reasons:

  • Since solid-refresh doesn't do cross-file analysis, there is no way for it to know that ResultKind is a const enum (or even just enum)
  • TypeScript seems to ignore this fact too, but it does raise a compiler warning.

Take note that the transformation happens before TypeScript gets to compile anything.

Currently, there is no fix for this issue (until we do cross-file analysis, but I don't see it happening any time), so I would recommend using some alternatives (either use enum, which is bad, as I am a hater of it myself or you use string unions)

lxsmnsyc avatar Jan 13 '25 10:01 lxsmnsyc

Makes sense, I can work-around for now. Thanks!

tmm1 avatar Jan 13 '25 18:01 tmm1