I've been experimenting with modules and came across a scenario that I'm struggling to fully understand. This is how I have everything set up:
import MyTesterImpl from "./MyTesterImpl";
declare module "./MyTesterImpl" {
interface MyTesterImpl {
augmented: boolean;
}
}
const main = async (): Promise<void> => {
let testy: MyTesterImpl = {
basicA: "hey",
basicB: "there"
} as MyTesterImpl;
testy.augmented = true;
};
main();
MyTestImpl is defined as follows:
export default class MyTesterImpl {
public basicA: string;
public basicB: string;
constructor(a: string, b: string) {
this.basicA = a;
this.basicB = b;
}
showStuff() {
console.log(`${this.basicA} ${this.basicB}`);
}
}
I am using the following tsconfig setup:
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"lib": ["dom", "es6", "es2017", "esnext.asynciterable"],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
"declaration": false,
"removeComments": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"baseUrl": ".",
},
"exclude": ["node_modules", "**/*.d.ts", "**/*.spec.ts"],
"include": ["./**/*.ts"]
}
Despite adding module augmentation to append the augmented field to the MyTesterImpl class, the compiler keeps throwing the error:
Property 'augmented' does not exist on type 'MyTesterImpl'.
You can access a reproducible example of the issue here.
I've spent hours trying to figure out why the error persists. I believe that I should be able to perform module augmentation between a class and an interface with the same name, so I don't think that's the problem. Also, I double-checked the class file path when declaring the module for augmentation.
Everything seems correct to me; what am I overlooking?