Marketto / codice-fiscale-utils

Utility to handle Italian Codice Fiscale
MIT License
25 stars 11 forks source link

Handle tsconfig.json `verbatimModuleSyntax` errors #103

Closed carmelone closed 5 months ago

carmelone commented 10 months ago

When using "verbatimModuleSyntax": true in tsconfig.json I get 74 errors using your amazing package.

All like this one:

Error: 'Genders' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
import Omocodes from "./enums/omocodes.enum";
import Genders from "./types/genders.type";

I think the solution is to change imports like this:

import Genders from "./types/genders.type";

to this:

import type Genders from "./types/genders.type";

Am I right?

carmelone commented 5 months ago

Dear @Marketto,

I opened this issue because in a new React or Svelte or Solid project (where the tsconfig is not decided by me but is written by a generator), if I use your project (@marketto/codice-fiscale-utils) I get 74 typescript errors of various types, like these:

c:\web\node_modules\@marketto\codice-fiscale-utils\src\date-utils\date-day.type.ts:6:16
Error: An 'export default' must reference a value when 'verbatimModuleSyntax' is enabled, but 'DateDay' only refers to a type.

export default DateDay;

c:\web\node_modules\@marketto\codice-fiscale-utils\src\date-utils\date-month.type.ts:3:16
Error: An 'export default' must reference a value when 'verbatimModuleSyntax' is enabled, but 'DateMonth' only refers to a type.

export default DateMonth;

c:\web\node_modules\@marketto\codice-fiscale-utils\src\date-utils\multi-format-date.type.ts:1:10
Error: 'Moment' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
import { Moment } from "moment";

...

and I'm importing your project like this:

import { Parser, Validator } from '@marketto/codice-fiscale-utils';

So to continue using it I opened a PR that would fix the problems not only with future projects but also with past ones (since in any case if you release a new major version or a feature version the user who updates knows that there are new features). (And in any case I don't see why old projects shouldn't be updated as well).

If you tell me that the problem is in my project and you are not willing to find a solution, our team will have to find an alternative solution unfortunately.

In any case: sorry if I bothered you.

carmelone commented 5 months ago

I cannot import it like this neither:

import CodiceFiscaleUtils from '@marketto/codice-fiscale-utils';

because of:

Module '"c:/web/node_modules/@marketto/codice-fiscale-utils/src/index"' has no default export. ts(1192)
Marketto commented 5 months ago

Hi @carmelone , Those changes break the unit tests and make the lib not backward compatible with non-verbatim projects. A verbatim project doesn't need verbatim dependencies

Using VermatimModuleSyntax you just need to import as follows

import * as CodiceFiscaleUtils from '@marketto/codice-fiscale-utils';
import type {BelfiorePlace, DateDay, DateMonth, Genders, MultiFormatDate} from '@marketto/codice-fiscale-utils';

No Changes are needed to the source code I will add this import hint in the README.MD I'll check the possibility to include default export in the next release I tried a demo project with success, please let me know if it solves your issue

carmelone commented 5 months ago

Thanks for answering me.

Even if I use:

import * as CodiceFiscaleUtils from '@marketto/codice-fiscale-utils';

I'm still getting those typescript errors.

I created a reproduction for you. Steps:

  1. Open https://stackblitz.com/edit/sveltejs-kit-template-default-deqtfq

  2. Run npm run check in the Terminal

  3. You should see the typescript errors in Terminal

I don't understand why you don't want to add those changes.

Can we fix the unit tests?

Marketto commented 5 months ago

Ok, problem found and managed, the fix will be available in the next release (I'll try to release in the next days) Sorry for the misunderstanding, but next time please use the bug report template. The problem was related to the "typings" in the package.json pointing to the source code, the correct solution is to generate an isolated typings.d.ts which prevents dependand project compiler to consider the lib source code as part of the code to be checked. (I reviewed the sourced code with "import type" for quality and reliability reasons)