I'm trying to use the format
function of the Node.js module date-fns
.
Some info about my system (obtained by the command ng --version
) is reported below:
Angular CLI: 13.2.0
Node: 16.13.2
Package Manager: npm 8.6.0
OS: linux x64
I have installed date-fns
by the command:
npm install date-fns
I have written an example file test_typescript.ts
with the following code:
import { format } from 'date-fns';
const currentDate: Date = new Date();
const formattedDate: string = format(currentDate, 'dd/MM/yyyy HH:mm');
console.log(formattedDate);
If I try to compile it by the following command:
tsc src/test_typescript.ts
I obtain the following error TS1005
:
node_modules/date-fns/clamp.d.ts:18:59 - error TS1005: '?' expected.
18 Options extends ClampOptions<infer DateType extends Date>
~
node_modules/date-fns/closestTo.d.ts:18:63 - error TS1005: '?' expected.
18 Options extends ClosestToOptions<infer DateType extends Date>
~
node_modules/date-fns/interval.d.ts:21:62 - error TS1005: '?' expected.
21 Options extends IntervalOptions<infer DateType extends Date>
~
Found 3 errors.
The problem is in the import
. How can I solve it?
date-fns
versionThanks to @jonrsharpe (see the comments to the question) I have solved my problem which is a compatibility conflict between the version of the module date-fns
and the version of the TypeScript compiler installed on my system.
In particular the TypeScript compiler version 4.5.5
doesn't support the version 4.1.0
of date-fns
(this is the date-fns
version released until today and this version has been installed by default by the command npm install date-fns
).
This is highlighted by the jonrsharpe's comment:
Looks like you need TS v5 to use date-fns v4 ...
So I have removed the installed module by the command:
npm uninstall date-fns
date-fns
to version 2.30.0
After that I have followed some of the steps described in this post.
The first step is to get the list of date-fns
versions available by the following command:
npm view date-fns versions
In the list there are many versions so by many downgrade attempts, which led me to other compilation errors, I have found that the newest version compliant with my TypeScript compiler (version 4.5.5) is the date-fns version 2.30.0
.
So I have installed it by the command:
npm install date-fns@2.30.0
After this downgrade I have been able to compile and execute my test file:
# compilation
tsc src/test_typescript.ts
# execution
node src/test_typescript.js
# output
23/10/2024 17:41