How Is 'this' Used In A Function And Is It Required?
Solution 1:
Regarding the question about any
, I wouldn't expect that to cause an actual error (by which I mean it won't transpile due to syntax errors), but I would expect a linter plugin to be complaining about it.
In general, you want to either use unknown
instead of any
if what you really mean is "I won't know until runtime what object actually gets bound to this type", because any
will be far too lenient in letting you reference members that may or may not exist, whereas unknown
is a little more strict (and type-safe).
Before resorting to unknown
however, it's generally preferable to define an actual type, even something like Record<string, string>
(an object with string
properties mapping to string
values). Or using a generic type if you need something a little more dynamic (although, not the case here).
In fact, recently typescript made a change where a caught exception is promoted to be unknown
as opposed to the former any
, due to type safety.
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-0.html#unknown-on-catch-clause-bindings
In general, avoid any
, and any good linter will tell you the same. I assume that's what your error was about. Correct me if I was mistaken, though.
Post a Comment for "How Is 'this' Used In A Function And Is It Required?"