How to define angular's @Input decorator in an interface?

Orlandster

Given is the following Angular component:

export class NumberComponent implements NumberInterface {
  @Input() number;
  constructor() { }
}

Now I'm looking forward to implement the NumberInterface. That's what I've got so far:

export interface NumberInterface {
  number: number
}

Basically, I'm using the type Function to define the Decorator. Now I'm wondering if there is a more specific way to do this. The interface should force the component to implement angular's @Input decorator.

joh04667

Interfaces in Typescript strictly do not have value, as they are entities that only exist for type-checking and are not compiled into JS.

You can't define a decorator in an interface because the decorator has value. It's a function that takes an argument and gives value to the class property, and not a type.

Angular's @Input decorators themselves do have a typing, but it's impossible to type them in a lower-order class since that property is itself already a type, but given value by the decorator:

export interface ITest {

  prop: Input;
  prop2: InputDecorator;

}

export class Test implements ITest {

  @Input('thing') prop: string;
  @Input('thing2') prop2: string;

}

// typeerror: Test does not correctly implement ITest

However, component classes can be derived from in Angular, and the parent class' decorators will be inherited by the child. This is useful if you want to implement strong typing alongside a more polymorphic approach to your code:

export class NumberBase {

  @Input('number') number: number;

}

export class NumberComponent extends NumberBase {


  ngOnInit() {
    console.log(this.number); // works!
  }

}

Components can extend either plain TS classes or fully decorated Angular components.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can you define a Decorator interface in TypeScript

How to use an Observable in Input Angular Decorator?

how to define the input and output for the interface in systemverilog

How angular's @Attribute decorator works?

How to define decorator within the class?

Generic type for @Input() decorator angular

When and how s decorator applied to the decorated classes from the @angular packages

How to define "interface" in js

How to define an interface with properties?

How to define a function's argument type as one of the keys (or properties) of an interface

React + Typescript: How to define interface for an array that's populated from componentDidMount?

How to define props interface for Apollo's React HOC?

Angular2 How to Define Input Property in Plain JS

@Input() Decorator and directives Angular-2

Angular2. Extending Input decorator

What signifies the @Input decorator on the component property in Angular

Angular 2+: Input decorator not reflecting checkbox

Angular removing an @Input decorator from code

Angular2-6, regexp and input decorator

I cant add the Input Decorator in Angular

How to correctly implement a decorator interface to an overloaded class

How to pass the value for the dropdown from using each method and input decorator in angular 14

How to define interface for object's every field must be instance of an other interface in TypeScript?

How to define an interface that ensures it's children and parent are of the same type as the defined interface

How to define an interface from another interface

How to define interface for class in TypeScript?

Static polymorphism: How to define the interface?

How to define a common interface in typescript

How to define a type a 'typeof this' for in the interface