Skip to content

Instantly share code, notes, and snippets.

@ichiroku11
Last active December 22, 2015 02:29
Show Gist options
  • Select an option

  • Save ichiroku11/6403881 to your computer and use it in GitHub Desktop.

Select an option

Save ichiroku11/6403881 to your computer and use it in GitHub Desktop.
Overload on Constants(定数によるオーバーロード)を試す Version:0.9.1.1
document.addEventListener("DOMContentLoaded", function(event) {
var toString = Object.prototype.toString;
// 文字列リテラルを使ってcreateElementを呼び出すと、HTMLAnchorElement
// オーバーロードが有効になる
var element1 = document.createElement("a");
// エラーにならない
element1.href;
console.log(toString.apply(element1)); // [object HTMLAnchorElement]
// 変数を使ってcreateElementを呼び出すと、デフォルトのHTMLElement
var tagName = "a";
var element2 = document.createElement(tagName);
// error TS2094: The property 'href' does not exist on value of type 'HTMLElement'.
//element2.href;
console.log(toString.apply(element2)); // [object HTMLAnchorElement]
// 関数の戻り値を使ってcreateElementを呼び出すと、デフォルトのHTMLElement
var func = () => "a";
var element3 = document.createElement(func());
// error TS2094: The property 'href' does not exist on value of type 'HTMLElement'.
//element3.href;
console.log(toString.apply(element3)); // [object HTMLAnchorElement]
});
@ichiroku11

Copy link
Copy Markdown
Author

Working on TypeScript 0.9: Generics, Overload on Constants and Compiler Performance - TypeScript - Site Home - MSDN Blogsより。

If you call it with a string expression other than a literal value (i.e. an identifier, the result of a function call, etc.) you’ll get the default ‘HTMLElement’.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment