microsoft / microsoft/TypeScript

Improve string split return type of first array index

オープン
#53,362 コメント 10 件 リアクション 6 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
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

https://github.com/microsoft/TypeScript/blob/b70784ef8f3ba3fd21750b9acf275dd7edcc7fe7/src/lib/es5.d.ts#L460

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split#description (note)

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず src/lib/es5.d.ts の460行目付近にある String.split の宣言から始め、次に関連する issue #49635 と、そこで参照されているコメントを確認してください。型がリテラルの空でない区切り文字について、最初の要素が空でないタプルを返すものとして区別しつつ、空の配列のケースを維持し、それらのケースをパラメーターの JSDoc に記載できれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, typescript
領域
compilers
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
48/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。