0

I'm learning about decorator in typescript.

In that book, decorator is a simple function implementation, and decorator can get only one parameter.

Right below.

function logger(arg:string){
    //decorator todo    
}

@logger("test")
class TestClass {}

In fact, this structure was seen in Angular too.

So I went to the definition of the most used "Component" in Angular.

But the definition of Component is interface.

Right Below is defined "Component" in Angular

directive.d.ts
//also Directive is interface too
export interface Component extends Directive {
    changeDetection?: ChangeDetectionStrategy;
    ....
}

Of course, it inherits several other interfaces, but the conclusion is that the definition of Component is interface.

I have used the same example in typescript without using Angular for testing.

interface Component{
    templateUrl?: string;
    template?: string;
    styleUrls?: string[];
}

function logger(name:string){
    return function(target:Function){
        return console.log(`class Name: ${name}`);
    }
}

//@logger("BOOK")
@Component({
    selector: 'bap-select-cscV2',
    templateUrl: './csc-v2.component.html',
    styleUrls: ['./csc-v2.component.css']
  })
class BOOK{
    constructor(private title: string){}
};

I have two questions here.

First, when I build that script into ng-node using @logger without using @Component, "Class Name: Book" is logged.

I understand that Decorator is metadata. I did not even create a class, but I do not know why the logger works.

Secondly, I dont know why interface(named Component) does not behave like an angular.

Error is : Component only refers to a type, but is being used as a value here.

1 Answer 1

1

The logger decorator prints out to the console because the function is being evaluated. The return console.log(``class name ${name}``); line gets evaluated. The decorators are a bit different from the Angular ones because Angular uses the reflect-metadata package for doing a reflection kind of thing.

The error you are getting is because you have declared an interface and are trying to use it as if it had been initialised. In TypeScript, when you define an interface, it is only used for TypeScript to figure out the types on an object. When the TypeScript gets compiled, interfaces don't get outputted.

The reason that when you look at the definition for the Component decorator in Angular is an interface is because they are purposefully hiding the implementation details from you. They call it a Facade. They don't want you to actually navigate to the code for it, rather, they want you to see what the "Shape"* of the Component object is.

This is usually a type definition file .d.ts that will tell TypeScript what the types are for everything in Angular.


*"Shape" just refers to what the object is comprised of (methods, properties, etc.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.