我們如何在環境TypeScript中聲明一個函數,該函數被調用this
設置爲特定類型?具有特定THIS類型的環境TypeScript函數
-1
A
回答
0
您可以在功能使用this: Type
作爲第一arugument:
class MyClass {
constructor(public myText: string) {
}
}
// enforce the context to be of type MyClass for this function
function myFunction(this: MyClass) {
alert(this.myText);
}
var myObject = new MyClass("Hello");
myFunction.call(myObject); // good
myFunction(); // bad
+0
我正在使用最新的TypeScript 1.8.10,並且不支持「this:MyClass」語法。我錯過了什麼? –
+0
你需要打字稿* nightlies *:https://basarat.gitbooks.io/typescript/content/docs/getting-started.html – basarat
相關問題
- 1. 具有requirejs的環境特定變量
- 2. pytest - 設置具有環境特定值的環境變量
- 3. Typescript - 遞歸函數類型
- 4. Typescript - 高階函數類型
- 5. Typescript禁用具有類的特定ID內的所有元素
- 6. TypeScript:可變類函數的類型
- 7. 函數模板:具有類型特徵的不同特化
- 8. 將具有子函數的WinJS函數定義轉換爲TypeScript
- 9. 具有可變數量/參數類型的函數的TypeScript聲明文件
- 10. 具有可變參數計數的函數的TypeScript類型簽名
- 11. Typescript中的Cast函數參數類型
- 12. typescript - 使用類型化函數從混合數組返回特定類實例
- 13. 具有類函數的類型轉換
- 14. 環境變量TypeScript
- 15. 如何爲具有特定類型特徵的所有類型編寫函數模板?
- 16. Typescript具有通用參數/返回類型的React無狀態函數
- 17. TypeScript:函數類型之間的區別
- 18. TypeScript交集類型的函數
- 19. TypeScript中的函數類型警衛
- 20. 具有語言環境特定設置的格式編號
- 21. One Typescript函數參數 - 可選類型?
- 22. Swift - 具有特定類別類型的數組
- 23. 環境決定論的類型
- 24. TypeScript中的環境變量
- 25. 創建具有特定格式的自定義數據類型
- 26. 具有特定葉子類型的嵌套對象的類型
- 27. 使用TypeScript指定函數參數的類型
- 28. 函數覆蓋,正在返回具有特定類型的向量
- 29. Promise.prototype.finally的TypeScript類型定義
- 30. TypeScript BigNumber的類型定義
的可能的複製[?有可能在功能方面的限制(http://stackoverflow.com/questions/21519547/is -a-constraint-on-function-context-possible) –