Type aliases to `any`

13.01.2019

any solves any problem

Cicerone

As you know, anyconsideredharmful in TypeScript. I do believe it’s better to avoid it, and especially after unknown was introduced to replace any in lots of cases. To discourage developers from using it, I can even imagine no-any rule turned on as a warning, but I don’t think deprecating any should be a goal.

Having a number of dependencies, it might be problematic to integrate all the third-party code without using any. Sometimes, when you upgrade a single package, some of its’ types rely on other package’s types, so you have to add a sprinkle of any. Or, pretty commonly, every TypeScript upgrade brings a handful of new errors across whole codebase. And if you don’t have a full picture or even authority to make substantial changes in someone else’s code, at times you have to suppress these errors with another splash of any. Thus, maintaining zero-any codebase seems to me excessively and unduly time consuming.

But we still want to discourage people from using it, and that’s where type aliases to any became handy:

/** */
type temporaryAny = any;

temporaryAny solves refactoring problems mentioned above by not using any and simplifying spotting refactoring-only any.

Once you see the benefits, you realise there is nothing that stops you from annotating every non-semantic any with temporaryAnyUntillCertainProblemSolved:

/**
* Type alias for string types that have to be narrowed to literal types
*/typetemporaryString=string;/**
* Type alias for number types that have to be narrowed to literal types
* https://github.com/Microsoft/TypeScript/issues/15480
*/typetemporaryNumber=number;/**
* Used for situations when conditional types (for example `ReturnValue<>`) should be used.
*/typetemporaryAnyUntilTypescript2_8=temporaryAny;