1

I am trying to use generic type in @Input in angular component like this, @Input('list') list: <T>;

this is not working '(' expected.ts(1005) Public property 'list' of exported class has or is using private name ''.

Please help, how can I do it.

2
  • Use @Input('list') list: T; Commented Sep 18, 2020 at 10:28
  • @fridoo, its giving error , Cannot find name 'T'.ts(2304) Public property 'list' of exported class has or is using private name 'T' Commented Sep 18, 2020 at 10:34

1 Answer 1

2

Make your Component generic

@Component({
  selector: 'hello',
  template: `<h1>Hello {{name}}!</h1>`,
  styles: [`h1 { font-family: Lato; }`]
})
export class HelloComponent<T>  {
  @Input() name: T;

  ngOnInit() {
    console.log('hello', typeof this.name)
  }
}

https://stackblitz.com/edit/angular-ivy-khusgc?file=src/app/hello.component.ts

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

1 Comment

I also want to mention, that it is necessary to add <T> at the end of your class-/componentname. I overlooked that part

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.