saghul / txiki.js

A tiny JavaScript runtime
MIT License
2.37k stars 157 forks source link

Replaced `any` with `unknowns` in txikijs.d.ts #522

Closed KaruroChori closed 1 week ago

KaruroChori commented 2 weeks ago

unknown in my opinion better matches the semantic of ts types for this situation. Functionally there is no difference since the code implementing that interface is written in js.

lal12 commented 1 week ago

Hmm I don't necessarily agree. The only real advantage of unknown is when using the unknown value, for a parameter it is pretty much useless. Also from the perspective of the user/caller passing "unknown" doesn't really make sense, but passing "any" value is clear. Other typings like the ts libs and @types/node also use any.

KaruroChori commented 1 week ago

When using any you are effectively telling the type system that you are giving up on characterizing it AND any later code should disregard that as well. On the implementation side, it means you can use those variables in any way you want, and no static check is going to be performed at compile time. Using unknown means there is a lack of information about its type, but at some point the implementation is forced to perform a cast and take responsibility of that in order to perform any meaningful operation. Clearly the library is written in JS, so the two options are behaviorally equivalent, but their semantic is hardly the same.

The fact other libraries are using any in place of unknown does not make it more or less right to be honest. Historically speaking unknown is way more recent as it was introduced by ts 3.0. So it does make sense a lot of the practice around it and the libraries already established were using any and are still doing it to this day. I have the feeling that if we were to compare the respective usage in interfaces between node and a more recent runtime like bun, the difference in usage would be more visible, but I have no data to support that.

Incidentally, this is why virtually all modern linters are so opposed to the usage of any in most default configurations.

lal12 commented 1 week ago

Yeah I get that / know the difference. I just didn't see how it is useful (even just semantically) for an API typing.

But I didn't really consider that this where callbacks which can be user provided, so yes unknown definitely would be useful there. I looked at deno typings, they also use unknown for such cases, though they use any for API where the user does not have to interact.

saghul commented 1 week ago

Thanks for taking a look @lal12 !