TypeScriptバージョン: 3.4.0-dev.201xxxxx
検索ワード:
マップされたタイプのキー順序が失われました
コード
type Initial = [1, 2, 3, 4]
type FindReplace<T extends any[], Find, Replace> = {
[K in keyof T]: T[K] extends Find ? Replace : T[K]
}
// Map over array and replace '2'
type ResultArray = FindReplace<Initial, 2, 'was 2'>
// => [1, "was 2", 3, 4]
// Now accessing the array values using [number], the type
// order is lost. This matters because now when the union is later
// cast as a intersection, the types are in the wrong order
type Result = ResultArray[number]
// => 1 | 3 | 4 | "was 2
予想される行動:
タイプをマッピングすると、キーの順序が保持されます。
実際の動作:
タイプをマッピングすると「タイプ」は保持されますが、 T[keyof T]
演算子を実行するとキーの順序が失われます
関連する問題:
最も参考になるコメント
連合の秩序はあなたが何のために頼るべきものではありません。 @RyanCavanaughは、タプルへのここで議論さました: