Imagine a scenario where a class adds itself to another class in the following manner
bar.ts
:
import { Foo } from './foo';
export class Bar {}
Foo.prop = Bar;
Now, consider the file foo.ts
export class Foo {
public static prop: any;
}
If we want to utilize this in index.ts
import { Foo } from './foo';
console.log(Foo.prop); // -> undefined
The value returned is undefined. It seems like bar.ts
is not being utilized at all (possibly due to tree shaking). To address this, it can be corrected as follows:
import { Foo } from './foo';
import { Bar } from './bar';
new Bar(); // trick!
console.log(Foo.prop); // -> Bar
Is there a method to instruct typescript to include Bar
regardless, as the current solution may seem cumbersome.
For reference, here is my tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"sourceMap": true,
"noImplicitAny": false,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2017",
"outDir": "./dist",
"baseUrl": "./",
"lib": ["es2017", "dom", "dom.iterable"]
},
"exclude": ["node_modules", "**/*.spec.ts", "dist"]
}