Skip to content

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

Merged
saghul merged 1 commit intosaghul:masterfrom
KaruroChori:patch-1
May 21, 2024
Merged

Replaced any with unknowns in txikijs.d.ts#522
saghul merged 1 commit intosaghul:masterfrom
KaruroChori:patch-1

Conversation

@KaruroChori
Copy link
Copy Markdown
Contributor

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.

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.
@KaruroChori KaruroChori changed the title Replaced any with unknowns in txikijs.d.ts Replaced any with unknowns in txikijs.d.ts May 18, 2024
@lal12
Copy link
Copy Markdown
Contributor

lal12 commented May 19, 2024

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
Copy link
Copy Markdown
Contributor Author

KaruroChori commented May 19, 2024

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
Copy link
Copy Markdown
Contributor

lal12 commented May 19, 2024

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 saghul merged commit dd81592 into saghul:master May 21, 2024
@saghul
Copy link
Copy Markdown
Owner

saghul commented May 21, 2024

Thanks for taking a look @lal12 !

@KaruroChori KaruroChori deleted the patch-1 branch May 21, 2024 07:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants