2017-12-03 111 views
0
//ts code 
function div(props?: { id?: String; style?: any }) { 
     return function(...children: ReactNode[]) { 
     return createElement("div", props, ...children); 
     }; 
    } 
const d = div({ id: "hello" })("welcome to TS"); 

生成的JS代碼打字稿編譯器是否有@inline功能選項?

function div(props) { 
    return function() { 
     var children = []; 
     for (var _i = 0; _i < arguments.length; _i++) { 
      children[_i] = arguments[_i]; 
     } 
     return createElement("div",props,...); 
    }; 
} 
var d = div({ id: "hello" })("welcome to TS"); 

//努力實現

var d = createElement("div",{ id: "hello" },"welcome to TS") 

確實打字稿支持@inline的功能呢?如果不是最好的方式實現類似的..

+2

不,在打字稿內聯函數的支持,你應該相信JS運行時做了優化,或手編代碼你想要的方式。 –

+1

要添加到以前的評論,請閱讀[「JavaScript引擎中的自動內聯」](https://ariya.io/2013/04/automatic-inlining-in-javascript-engines)---如果您的代碼是如果您希望手動優化這些細節,您應該查看WASM(WebAssembly),這正是它的一個用例([Youtube:Dan Callahan:實用WebAssembly | JSConf布達佩斯2017年](https://www.youtube。 com/watch?v = bac0dGQbUto) - 跳過第15分鐘)。 –

+0

感謝您的信息:) – invariant

回答

1

不打字支持@inline函數嗎?

如果不是最新最好的方式實現類似

你將不得不自己編寫的工具。我只是不擔心它。

使刀具自己

使用打字稿編譯器找到您要內聯函數的引用,然後函數體插值到那個位置。

開始:一些編譯器文檔https://basarat.gitbooks.io/typescript/content/docs/compiler/overview.html

+0

感謝您的回覆,您可以將我鏈接到一些文章/教程/文檔如何實現類似的東西...,即使它對這種用例沒有用,我可能會在未來遇到不同的需求。 。 – invariant

+0

爲此增加了更多信息 – basarat