I am trying to add a global prototype function to my Angular-App, but it throws an error during compilation:
./src/app/helpers/functions.ts - Error: Module build failed (from ./node_modules/@ngtools/webpack/src/ivy/index.js):
Error: \src\app\helpers\functions.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
at \node_modules\@ngtools\webpack\src\ivy\loader.js:59:26
I import the file in the app/app.module.ts the following way (1st line):
import './helpers/functions.ts';
Object.prototype.clone = clone;
Array.prototype.clone = clone;
declare global {
interface Array {
clone(): typeof clone;
}
interface Object {
clone(): typeof clone;
}
}
function clone() {
// creates deep copy of "this"
}
Maybe someone has an idea how to solve this? I appreciate your help!
Object.prototypeorArray.prototypevia direct assignment like that, it creates an enumerable property, which will break lots of code that expects objects inheriting from those not to have any enumerable properties. Some would say never extend those prototypes at all, but if you're going to, useObject.definePropertywith the defaultenumerableflag (which isfalse), so that the property isn't enumerable. But I'd be surprised...clone. If you don't know that none of the 3rd party code you're using has acloneproperty on an object somewhere, you're really better off just using a utility function you pass the object into. :-)tsconfig.app.json, but your notes became true after the prototype function was imported. So I will just export a clone-function :)