I have been working on developing my own Object-Relational Mapping (ORM) system and I encountered an interesting issue in TypeScript that I am trying to understand. If anyone can provide some insight or clarification on this matter, it would be greatly appreciated.
Let me share a simplified version of the basic structure where this peculiar behavior occurs.
There is a property decorator that accepts a parameter of a class type.
// This is optional but helps in explicitly defining the entityClass type
export abstract class Entity { }
export function Decorator(entityClass: typeof Entity) {
return function(target: any, propertyName: string) {
console.log(entityClass);
}
}
File containing Class A: EntityA.ts
import { Entity, Decorator } from "./index";
import { EntityB } from "./EntityB";
export class EntityA extends Entity {
@Decorator(EntityB)
public propertyA: any;
}
File containing Class B: EntityB.ts
import { Entity, Decorator } from "./index";
import { EntityA } from "./EntityA";
export class EntityB extends Entity {
@Decorator(EntityA)
public propertyB: any;
}
A snippet of code to invoke TypeScript to utilize the decorator for logging purposes:
run.ts
import { EntityA } from "./EntityA";
import { EntityB } from "./EntityB";
const objA = new EntityA();
const objB = new EntityB();
When running this script, the output is as follows:
undefined
[Function: EntityB]
The occurrence of undefined
is what I find intriguing.
If I remove @Decorator(EntityB)
line from EntityA.ts and run the script again, the undefined
disappears yielding the expected result:
[Function: EntityA]
Am I misunderstanding something about decorators, or could this be a potential bug in TypeScript?