deathangel908
deathangel908

Reputation: 9699

Typescript lint what's the proper way to declare any type with "no-any": true

I want to use gts in my project.

It has the rule in tslint.json "no-any": true. That rule totally forbids using any keyword anywhere in the code. What's the common use case of dealing with it function can accept any type. I'm aware that I can override this rule. But I wonder how would someone handle the situation when e.g. function that executes sql accepts arguments of different types. Is there best practices to handle this case?

The only thing that comes in mind is to specify all primitives types via |

param: string| number| Date;

But what if I'm wrapping some external code e.g. mysql in my function that already accepts any[].

Upvotes: 1

Views: 2511

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249506

I think most cases should be covered well by unknown. unknown is a safer alternative to any. The basic idea is you can assign anything to unknown just like any, but unlike any you can't do much with unknown without explicit checks or type assertions which is probably what you want (see here for details).

You might still encounter corner cases where unknown can't just replace any (type parameters with function constrains under strictFunctionTypes come to mind), but it should generally work.

Upvotes: 2

Related Questions