'How to use deep-equal (Angular 7)
Auto import by VS Code:
import deepEqual = require('deep-equal');
Doesn't work:
error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
Then I tried to do like this:
import * as deepEqual from 'deep-equal';
error TS2497: Module '"path/@types/deep-equal/index"' resolves to a non-module entity and cannot be imported using this construct.
And like this:
import {deepEqual} from 'deep-equal';
error TS2305: Module '"C:/Projects/ManagerServer/Src-LoyaltyCoin.ManagerServer.Core/ManagerWeb/ClientApp/node_modules/@types/deep-equal/index"' has no exported member 'deepEqual'.
I haven't got ideas how can I import this. If there's no solution for this, please suggest me another library to compare objects for Angular 7.
Solution 1:[1]
Add the script to your angular.json
file :
scripts: [
"node_modules/deep_import/name.of.minified.file.js"
]
And declare a variable in your component :
import { Component } from '@angular/core';
....
declare const deepEqual: any;
Or, you can find typings for it and install them, and import it like any other dependency :
import * as deepEqual from 'deep-equal';
EDIT Just a syntax issue. I suggest you open the repository and see the syntax. The correct one is
import deepEqual from 'deep-equal';
as you can see in this stackbltiz
Solution 2:[2]
You may use fast-deep-equal
package. It's included as peer dependency by Angular, but don't forget to add it to regular dependencies.
Also, it's the fastest object comparer.
Usage
import equal from 'fast-deep-equal';
equal(a,b); // returns true\false
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |