I'm attempting to apply a decorator to seal
my class object, but it seems like it's not working. Even after initializing the class, I am able to delete a property or add another one to the class. Here is the code snippet:
function sealed(constructor: Function) {
Object.seal(constructor);
Object.seal(constructor.prototype);
}
@sealed
class BugReport {
type = 'report';
title: string;
constructor(t: string) {
this.title = t;
}
}
const bug = new BugReport('but');
delete bug.title;
delete bug.type;
console.log(bug); // BugReport {}
However, when I tried sealing a pure object, everything worked as expected. Here is the code snippet for that:
const obj = {
name: 'Jack',
};
Object.seal(obj);
delete obj.name;
console.log(obj); // will throw an error: Cannot delete property 'name'
This issue arises in the tsconfig.json
file as well.
{
"compilerOptions": {
// `target` and `lib` match @tsconfig/bases for node12, since that's the oldest node LTS, so it's the oldest node we support
"target": "es2019",
"lib": ["es2019", "es2020.promise", "es2020.bigint", "es2020.string", "dom"],
"rootDir": "src",
"outDir": "dist",
"module": "commonjs",
"moduleResolution": "node",
"strict": true,
"declaration": true,
"sourceMap": true,
"inlineSources": true,
"types": ["node"],
"stripInternal": true,
"incremental": true,
"skipLibCheck": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"strictPropertyInitialization": false
},
"ts-node": {
"swc": true
},
"include": ["src/**/*"],
"typedocOptions": {
"entryPoints": ["./src/index.ts"],
"readme": "none",
"out": "website/static/api",
"excludeTags": ["allof"],
"categorizeByGroup": false,
"categoryOrder": ["Basic", "REPL", "Transpiler", "ESM Loader", "Other"],
"defaultCategory": "Other"
}
}
Why is the decorator function unable to manipulate the class at all?
Appreciate any assistance provided.