2

In an Angular 6 project I'm trying to use the npm package object-set-all-values-to 3.9.45. I did the following:

1- Installed it using npm i object-set-all-values-to ✔️ OK

2- Tried to import it ❌ Error

  • 1st option

import setAllValuesTo from 'object-set-all-values-to';

Could not find a declaration file for module 'object-set-all-values-to'. '<my-project-path>/node_modules/object-set-all-values-to/dist/object-set-all-values-to.cjs.js' implicitly has an 'any' type.
  Try `npm install @types/object-set-all-values-to` if it exists or add a new declaration (.d.ts) file containing `declare module 'object-set-all-values-to';`
  • 2nd option:

import setAllValuesTo from 'object-set-all-values-to/dist/object-set-all-values-to.esm.js';

Could not find a declaration file for module 'object-set-all-values-to'. '<my-project-path>/node_modules/object-set-all-values-to/dist/object-set-all-values-to.esm.js' implicitly has an 'any' type.
  Try `npm install @types/object-set-all-values-to` if it exists or add a new declaration (.d.ts) file containing `declare module 'object-set-all-values-to/dist/object-set-all-values-to.esm.js';`

So, How can I solve this issue


This alternative (const setAllValuesTo = require('object-set-all-values-to');) works but I cannot use it because of strict code-styles rules in my project.


I already tried without success what is proposed to solve similar issues in:

For example, put above the import line // @ts-ignore and also to declare in a src/typings.d.ts file the module:

declare module 'object-set-all-values-to' {
    export default function setAllValuesTo(inputOriginal: any, valueOriginal: any): any
}

These give me the following error: object_set_all_values_to_1.default is not a function.

Also, I tried npm install @types/object-set-all-values-to but it seems there is not types defined for it because I get npm ERR! code E404.


Some possibly relevant config values in tsconfig.json are:

{
    "compilerOptions": {
        // ...
        "lib": [
            "dom",
            "es2018.promise",
            "es2015"
        ],
        "moduleResolution": "node",
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": true,
        // ...
    }
}

I tried changing noImplicitAny to false and the error reported is gone but the I get the mentioned object_set_all_values_to_1.default is not a function


I reported already the issue to the package author but got no answer so far.

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
  • 1
    Works fine for me in Angular 6.1.10 without any additional changes: https://i.imgur.com/xnKW73A.png. What do your tsconfig.json and tsconfig.app.json files look like? What's your exact Angular version? – Gosha_Fighten Oct 15 '19 at 13:51
  • @Gosha_Fighten interesting! Got to check the whole config, I'm trying to use it in a HUGE project. – lealceldeiro Oct 15 '19 at 13:52
  • @Gosha_Fighten The Angular version is `6.1.10`. Should I look for any specific config inside `tsconfig.json` and `tsconfig.app.json`? – lealceldeiro Oct 15 '19 at 13:56
  • First, I'd check what value the `compilerOptions.module` option has in tsconfigs. – Gosha_Fighten Oct 15 '19 at 14:16
  • where did ou create your typings.d.ts file with the declaration?? – Lucas Oct 15 '19 at 14:23
  • @Gosha_Fighten `compilerOptions.module` is `commonjs` ... updated the post. – lealceldeiro Oct 15 '19 at 14:30
  • @FRECIA `typings.d.ts` is inside `src` (`/src/typings.d.ts`). I updated the post... I don't think the place of `typings.d.ts` is the problem. Do yo have anything in mind? – lealceldeiro Oct 15 '19 at 14:31

3 Answers3

2

If compilerOptions.module is commonjs, an UMD module is used. In this case, import this library as

import * as setAllValuesTo from 'object-set-all-values-to';
lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
Gosha_Fighten
  • 3,838
  • 1
  • 20
  • 31
  • You were right about the import syntax. This solved the error `object_set_all_values_to_1.default is not a function`. Additionally I had to use the `// @ts-ignore` to suppress the compiler error because I cannot change the `tsconfig.json` – lealceldeiro Oct 16 '19 at 06:12
1

Looking at your config, I believe the issue is with the module definition config being set to commonjs, change it to esnext:

"module": "esnext"
Lucas
  • 9,871
  • 5
  • 42
  • 52
  • Nope... didn't work.... this is driving me nuts.... actually I created a sample Angular project from scratch to trys this npm package independently and it works out of the box. There must be some misconfiguration in this huge project I'm working on :( Thank you very much for the effort – lealceldeiro Oct 15 '19 at 16:19
  • 1
    That's too bad. Pleas post the fix when you find it, I'm really intrigued now. – Lucas Oct 15 '19 at 20:02
  • 1
    [Gosha_Fighten's answer](https://stackoverflow.com/questions/58395908/error-using-es-module-import-for-object-set-all-values-to/58397541#58397541) solved it. In any case your help was very useful too. Thanks – lealceldeiro Oct 16 '19 at 06:14
1

You should have to import the object_set_all_values_to_1.default. It's not a function as you declare it.

import * as setAllValuesTo from 'object-set-all-values-to';
smita
  • 298
  • 1
  • 4
  • 17