0
A
回答
1
不。作爲@AsadSaeeduddin說,這個請求在Microsoft/TypeScript#6229跟蹤。
如果你真的想要他們,你可以定義自己的「封閉」的元組:
interface Closed1<T> { 0: T }
interface Closed2<T, U> extends Closed1<T> { 1: U }
interface Closed3<T, U, V> extends Closed2<T, U> { 2: V }
interface Closed4<T, U, V, W> extends Closed3<T, U, V> { 3: W }
// as many as you like ...
function closeTuple<T, U, V, W>(open: [T, U, V, W]): Closed4<T, U, V, W>;
function closeTuple<T, U, V>(open: [T, U, V]): Closed3<T, U, V>;
function closeTuple<T, U>(open: [T, U]): Closed2<T, U>;
function closeTuple<T>(open: [T]): Closed1<T>;
function closeTuple(open: any): any {
return open;
}
function openTuple<T, U, V, W>(closed: Closed4<T, U, V, W>): [T, U, V, W];
function openTuple<T, U, V>(closed: Closed3<T, U, V>): [T, U, V];
function openTuple<T, U>(closed: Closed2<T, U>): [T, U];
function openTuple<T>(closed: Closed1<1>): [T];
function openTuple(closed: any): any {
return closed;
}
// demo usage
const anOpenTuple: [string, boolean, number] = ['foo', true, 2];
const okay = anOpenTuple[2]; // number
const what = anOpenTuple[10]; // string | number | boolean
const aClosedTuple = closeTuple(anOpenTuple);
const stillOkay = aClosedTuple[2]; // number
const nowError = aClosedTuple[10]; // error if you have noImplicitAny on
然後你就可以當你需要打開和關閉元組之間的轉換。然而,一旦你開始了這條道路,你可能會發現,最好是創建具有特定命名屬性的接口,而不是完全依賴元組。但你的里程可能會有所不同
希望有幫助;祝你好運。
相關問題
- 1. 爲什麼需要額外的對象?
- 2. 爲什麼我不能直接訪問帶索引的數組?
- 3. 爲什麼ng-class甚至需要額外的引號
- 4. INotifyProperyChanged - 爲什麼要額外分配?
- 5. Python按值訪問元組索引
- 6. 爲什麼按索引訪問這個json子元素引發這個錯誤?
- 7. 爲什麼Python在split()返回的數組中添加額外的引號?
- 8. 爲什麼Scala元組的索引爲1?
- 9. 爲什麼在這個vim errorformat中需要額外的轉義?
- 10. 在Python中,爲什麼itertools.cycle需要額外的內存?
- 11. 爲什麼GCC在模板中需要額外的聲明?
- 12. 爲什麼在這種情況下需要額外的檢查?
- 13. 爲什麼在僞元素周圍有額外的空間?
- 14. 爲什麼$ modal爲表單引入一個額外的範圍?
- 15. 爲什麼要從外部類訪問受保護的成員?
- 16. 爲什麼數字上的索引大於String上的索引?
- 17. 訪問數組索引
- 18. jquery訪問數組索引
- 19. 從索引訪問數組
- 20. 爲什麼你想要在視圖上放置索引?
- 21. 爲什麼不能通過C#中的索引訪問單個System.Array元素?
- 22. 爲什麼我的二進制搜索需要額外的比較? log2(N)+1
- 23. 訪問嵌套在字典中的元組的索引
- 24. Swift語法:在setter中訪問的數組元素的索引?
- 25. 斯卡拉爲什麼不元組索引爲零
- 26. 關於ipad上的Websockets,有什麼額外的需要完成?
- 27. MySQL:索引額外字段有什麼缺點?
- 28. 爲什麼Ruby數組允許訪問無效的範圍索引?
- 29. 爲什麼從構造中傳遞指針需要額外的解除引用?
- 30. 爲什麼在C++ 3d數組列表分配中需要額外支架?
您可能想投票支持:https://github.com/Microsoft/TypeScript/issues/6229 –