g45t345rt/minifaker

No typescript declaration files for locales

IanVS opened this issue · 7 comments

IanVS commented

In the published module, there are no typescript definitions for the locales. This is a problem because I am unable to use the side-effect import of 'minifaker/dist/locales/en' to set a locale, for some reason. It only seems to work if I follow this pattern:

import { addLocale, word, WordType } from 'minifaker';
import en from 'minifaker/dist/locales/en';

addLocale('en', en);

But then I get a typescript error.

Typescript for locales are not generated because you don't have to use addLocale.

import { word, WordType } from 'minifaker'
import 'minifaker/dist/locales/en'

word()

Here is where it's added when importing the locale

minifaker.addLocale('en', locale)

The addLocale function is if you want to create your own custom locale.

What is the Typescript error?

IanVS commented

I don't get a typescript error if I use the approach you mention, with the side-effect import. I get an error from word that a locale has not been set.

I'll try to make a reproduction to share.

Added type for locale

a6c1745be94fb3ffe9e4ee8bd4944637

IanVS commented

Thanks! Here's a reproduction of what I see when using the side-effect import:

https://stackblitz.com/edit/node-z2ajob?file=index.ts

IanVS commented

I tried out the new version, and I still get an error, I think because the module is not defined as having a Locale for a default export.

image

Thanks a lot for the repro! I was able to fix it.

I had to seperate both syntax in there own unique folder dist/esm, dist/cjs. The exact problem was that the addLocale while using side-effect import was resolving to the cjs file and the word function was using the esm file. They don't share the same locales :S

Plus, I added the NPM exports for locales. You don't have to use dist anymore :)

import 'minifaker/locales/en'

minifaker/package.json

Lines 8 to 28 in 6664ed5

"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
},
"./locales/en": {
"import": "./dist/esm/locales/en/index.js",
"require": "./dist/cjs/locales/en/index.js"
},
"./locales/fr": {
"import": "./dist/esm/locales/fr/index.js",
"require": "./dist/cjs/locales/fr/index.js"
},
"./locales/fr-CA": {
"import": "./dist/esm/locales/fr-CA/index.js",
"require": "./dist/cjs/locales/fr-CA/index.js"
},
"./locales/es": {
"import": "./dist/esm/locales/es/index.js",
"require": "./dist/cjs/locales/es/index.js"
}

IanVS commented

Awesome, thanks! I'll give the new version a shot tonight or tomorrow.