microsoft / microsoft/TypeScript
Improve string split return type of first array index
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
lib Update Request
Please, consider improving user experience for "".split(...) in cases when string literal is passed, which I believe most users do e.g. split(' ') or split('\n')
Configuration Check
My compilation target is ES2015 and my lib is the default.
Missing / Incorrect Definition
Essentially duplicate of https://github.com/microsoft/TypeScript/issues/49635, but with https://github.com/microsoft/TypeScript/issues/49635#issuecomment-1163047914 handled:
split<T extends string | RegExp>(separator: T, limit?: number): T extends `${string}${infer U}` ? [string, ...string[]] : string[];
And this should added to param JSDoc then: Only passing `""` and `new RegExp("")` values can result in returning an empty array.
Sample Code
This is super inconvenient when used with destructure:
const [firstLine, ...restLine] = str.split('\n')
// firstLine is possibly undefined, need add exclamation marks everywhere or `as [string, ...string]` above
I also wanted to do the same for when regexp literal is passed e.g. split(/\n\r?/) as only split(new RegExp('')) can result in empty array (if I'm not mistaken), which I believe no one does, but the literal value can't be captured anyway
Documentation Link
P.S. I remembered of this issue after https://github.com/microsoft/TypeScript/pull/49682, which significantly improved type-checking experience by using less exclamation marks
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず src/lib/es5.d.ts の460行目付近にある String.split の宣言から始め、次に関連する issue #49635 と、そこで参照されているコメントを確認してください。型がリテラルの空でない区切り文字について、最初の要素が空でないタプルを返すものとして区別しつつ、空の配列のケースを維持し、それらのケースをパラメーターの JSDoc に記載できれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 活発
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 48/100