Fork me on GitHub
#shadow-cljs
<
2024-02-21
>
andrea19:02:12

Is there any guidance on when type annotations are necessary? I have a piece of code that compiles for both dev and release without annotation, but works only in dev. In release it fails silently, which is scary and it took me a long time to debug. To make it work in release I have to add a type annotation. FWIW the object is the result of some js interop, but I wished that either it'd work as in dev or that I'd get one of those "Cannot infer target type in expression" kind of errors. Unfortunately the code is not in a sharable state at the moment, I'd need to do a bit of work to reduce it to a shareable example but maybe some of you can already recommend some strategy or knowledge that I'm missing re this failure mode.

thheller20:02:26

can you share the part of the code that had the problem?

thheller20:02:01

there are only a few known cases where externs inference fails and you don't get a warning (most are core.async related, but often still warn), and the only ones left I know of are usually "bad" code that use dotted symbols when they shouldn't

thheller20:02:46

you can run npx shadow-cljs release app --pseudo-names to make finding the problematic piece a bit easier

andrea21:02:17

It's in something like this, removing the Table annotation makes it fail.

^dexie/Dexie.Promise (.bulkAdd ^dexie/Dexie.Table (.-mytable ddb) my-vals)))
I'm also struggling with using -> rather than the the explict call and now wonder whether that's related.

andrea21:02:40

FWIW even --pseudo-names doesn't seem to make any difference

thheller22:02:22

pseudo names only makes the munged names recognizable, it is not meant to fix anything

thheller22:02:40

ok, so that ^dexie/Dexie.Promise tag is the problem, and should be just ^js

🙏 1
thheller22:02:13

^dexie/Dexie.Table same here. that is the exactly wrong kind of tag to use

thheller22:02:33

anything but js or js/Whatever will tell the compiler to NOT generate externs for this

andrea13:02:58

Thank you both, I've verified ^js works. I'm not sure why ^dexie/Dexie.Table is the exact wrong kind of tag and will have to read the extern docs again :)

thheller14:02:48

basically this externs inference stuff is really basic. you get a "cannot infer ..." warning if a property is encountered that is not recognized. so, you either tag is as js, telling the compiler to generate externs, or anything else and it won't. that is it really. it doesn't go as far as trying to resolve whatever the tag was and do any kind of type checking or so

❤️ 1
thheller14:02:56

it is far simpler than that 😛

thheller14:02:00

telling it exactly what type something is overkill if you ask me