If you’re creating code for other Node.js developers to use, chances are you’ll need a dash of run-time type checking. While TypeScript can help you with your types on a per-project basis you can’t force third party programmers to use it.
Node’s built-in util
module provides a number of useful methods for detecting the type of a variable. Many of the methods for detecting base javascript types (vs Node.js specific types) were deprecated in Node.js 4.0 (release in September of 2015). The methods still exist, but may disappear in a future Node.js release.
However — the deprecation docs include alternatives for identifying these types with native javascript code.
ex.
util.isArray(object)
Added in: v0.6.0 Deprecated since: v4.0.0
Stability: 0 - Deprecated: Use Array.isArray() instead.
I’m sure there’s tradeoffs to consider for each and everyone of these, but these suggestions provide a good starting point whenever you’re trying to decide which heuristic is the right one for identifying what’s in a particular javascript variable.