2012年12月7日金曜日

TypeScript での Type Assertion

  1. document.getElementById("#input");  
の戻り値は HTMLElement なので
  1. var input : HTMLInputElement = document.getElementById("#input");  
とすると、キャストできないと怒られます。
Cannot convert 'HTMLElement' to 'HTMLInputElement': Type 'HTMLElement' is missing property 'setSelectionRange' from type 'HTMLInputElement'

そこで、次のように < > を使ってキャストすれば怒られなくなります。
  1. var input : HTMLInputElement = <HTMLInputElement>document.getElementById("#input");  

0 件のコメント:

コメントを投稿