Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type of an Input field that is passed into a TypeScript function

I am following an Angular 2 tutorial in which an input element is passed to a function via click event.

The tutorial has an addTodo function with this signature addTodo(event, todoText){ }. This code is throwing up warning of Parameter 'todoText' has an implicit 'any' type.

I am wondering if there is a TypeScript object that that I can use in place of any that represents a form input control

Example Angular 2 HTML

<div class="add-todo-form text-center">
  <h1>Add ToDo</h1>
  <div class="form-group">
    <input class="form-control input-lg" placeholder="Add Todo.." autofocus #todoText>
    <br>
    <button (click)="addTodo($event, todoText)" class="btn btn-primary btn-block">Create</button>
  </div>
</div>

Example Typescript

addTodo(event: any, todoText: any){
  console.log('xxxxxxxxxxx');
  var result: any;

  var newTodo = {
    text: todoText,
    isCompleted: false
  };

  result = this._todoService.saveTodo(newTodo);

  result.subscribe( (x: any)=> {
    this.todos.push(newTodo);
    todoText.value = '';
  })
}
like image 677
David Cruwys Avatar asked Feb 19 '17 21:02

David Cruwys


2 Answers

The answer by @Gunter was close and helped lead me to what I wanted, I was able to drill down into lib.es6.d.ts to find HTMLInputElement which extends HTMLElement

like image 116
David Cruwys Avatar answered Oct 26 '22 16:10

David Cruwys


If it's a component or directive, then the instance of the component or directive is passed, otherwise HTMLElement

like image 21
Günter Zöchbauer Avatar answered Oct 26 '22 17:10

Günter Zöchbauer