microsoft / microsoft/TypeScript
Type `[x, x]` is not assignable to type `x[] & { length: 2; }`
オープン
まだ誰も着手していません。
In Discussion
Suggestion
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
Bug Report
This error is probably self-explanatory. Either this is supposed to work and it’s a bug, or I’m misunderstanding something in which case please feel free to close.
Type '(Record<string, any> | null)[] & { length: 2; }' is not assignable to type '[Record<string, any> | null, Record<string, any> | null]'.ts(2322)
Outer function is typed to return the latter. It calls an inner function, typed to return the former. One would assume this should work.
Possibly related: #44309
🔎 Search Terms
array literal length
🕗 Version & Regression Information
TypeScript 4.1.3
💻 Code
async function readObjectVersions
<L extends number, C extends string[] & { length: L }>
(objectPath: string, versions: C):
Promise<(Record<string, any> | null)[] & { length: L }> {
return versions.map(version => ({ foo: 'bar' }));
}
const version1: string = 'x';
const version2: string = 'y';
async function _readObjectVersions(objectPath: string):
Promise<[ Record<string, any> | null, Record<string, any> | null ]> {
return await readObjectVersions(objectPath, [version1, version2]);
}
_readObjectVersions('abc');
🙁 Actual behavior
The error as described above.
🙂 Expected behavior
No type error.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
提供された TypeScript サンプルで、TypeScript 4.1.3 を使用して readObjectVersions 関数と _readObjectVersions 関数のエラーを再現することから始めます。マップされた配列とその長さの制約が、タプルの戻り値の型に対してどのようにチェックされるかを追跡します。示された呼び出しが報告されたエラーなしで型チェックを通れば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 35/100