microsoft / microsoft/TypeScript
Infer non returning function to be returning `undefined`, not `void`
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
Search Terms
non returning, undefined instead of void, undefined or unknown instead of void
Suggestion
- Change return of non returning functions to
undefinedinstead ofvoid - Accept
unknownas a type of non returning functions
examples:
const f = () => {} // this gets inferred as () => void
const f1 = (): undefined => {} // doesn't work
// error: A function whose declared type is neither 'void' nor 'any' must return a value.(2355)
const f2 = (): unknown => {} // same error
Motivations
void is an annoying type to deal with, has many inconsistencies and there's a ton of issues around it for its weird behavior like: #35850 , #35236 , #33420 ... etc
Non-returning functions actually return undefined which is a more predictable type to deal with and narrow disjunctions with and even use optional chaining with.
Use Cases
Many use cases appear while using promises where there's a need to catch an error but at the same time not deal with void
const f = () => {}
async (p: Promise<A>) => {
const a = await p.catch(f);
// do stuff with a whose type is now A | void
};
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
Not sure if this might break some code or not but I think any code that dealt with void should work while dealing with undefined?
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
ソースファイルやテストは指定されていません。まず推論の例とリンク先の TypeScript playground を確認し、その後、戻り値を返さない関数を void から undefined に変更することで、promise と narrowing のユースケースにどのような影響があるかを調査してください。完了条件は、提案されている undefined と unknown の動作について、型システムの設計が確定していることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100