util.types Check Internal Slots
util.types checks internal slots, so date-like strings, regexp-like objects, and thenables fail their checks while the predicates still narrow types.
Code
// util.types checks internal slots, so look-alike values fail the check and
// the result doubles as a TS type predicate.
import { isDate, isRegExp, isPromise } from 'node:util/types';
const samples = [
['date', new Date('2026-01-01')],
['date-like string', '2026-01-01'],
['regexp', /cache-\d+/],
['regexp-like object', { source: 'cache-\\d+' }],
['promise', Promise.resolve('real')],
['thenable', { then: () => 'fake' }],
] as const;
for (const [label, value] of samples) {
console.log(
label,
'->',
'isDate:', isDate(value),
'isRegExp:', isRegExp(value),
'isPromise:', isPromise(value),
);
}
// The predicate form narrows unknown values without any casting
const values: unknown[] = [
'2026-01-01',
new Date('2026-01-01'),
{ then: () => 'fake' },
Promise.resolve('real'),
];
const dates: Date[] = values.filter(isDate);
console.log('narrowed dates:', dates.length);Output
Captured from a sandboxed run of this snippet in the build pipeline.
date -> isDate: true isRegExp: false isPromise: false
date-like string -> isDate: false isRegExp: false isPromise: false
regexp -> isDate: false isRegExp: true isPromise: false
regexp-like object -> isDate: false isRegExp: false isPromise: false
promise -> isDate: false isRegExp: false isPromise: true
thenable -> isDate: false isRegExp: false isPromise: false
narrowed dates: 1Inside the snippet
The file repeats itself only inside for (const [label, value] of samples). Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed. The only import source is node:util/types, a Node.js built-in, exposing isDate, isRegExp, isPromise.
The file’s state is a handful of top-level bindings: samples.
What to watch for
node:util/typesresolves inside Node.js without any package — it is a core module.
Putting it to use
The demo output is labeled (narrowed dates:), so each printed line maps back to the step that produced it.
Interactive Tools for This Topic
FAQ
- What does this import from outside the project?
- Only `node:util/types`, and that is a Node.js built-in — it comes with the runtime.
Report a problem with this exampleOpens a pre-filled issue on GitHub.