Nastya Scherbakova
Nastya Scherbakova

Reputation: 172

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.

Upvotes: 7

Views: 16488

Answers (2)

cuddlemeister
cuddlemeister

Reputation: 1785

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

Upvotes: 3

user4676340
user4676340

Reputation:

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

Upvotes: 10

Related Questions