microsoft / microsoft/TypeScript

`satisfies` for return types

オープン
#59,577 コメント 5 件 リアクション 18 件 担当者 0 名 GitHub で見る

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

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

🔍 Search Terms

satisfies
return

✅ Viability Checklist
⭐ Suggestion
type AcceptableType = string | number | boolean | undefined | null | object[]

/** `const fn: () => "asdf" | 8 | undefined` */
const fn = (): satisfies AcceptableType => {
  if (someCondition) {
    return "asdf"
  } else if(otherCondition) {
    return 8
  } else {
    return undefined
  }
}
📃 Motivating Example
function getResult(): satisfies { a: string, b: number } {
  // type-errors until `satisfies` condition is met
  return {
    // type-hints and type-checking for:
    // (property) a: string
    // (property) b: number
  }
}
💻 Use Cases

The satisfies operator has been immensely useful for ensuring expressions match some wider type while also inferring their narrower type

However, because satisfies works only on expressions, getting the same behavior on function return types requires workarounds with several drawbacks. For example, if a function has multiple return values, we need to type satisfies SomeType for every single one of them, which is not only cumbersome but also prone to human error

/** `const fn: () => "asdf" | 8 | undefined` */
const fn = () => {
  if (someCondition) {
    return "asdf" satisfies AcceptableType
  } else if(otherCondition) {
    return 8 satisfies AcceptableType
  } else {
    return undefined satisfies AcceptableType
  }
}

playground link

This style is also incompatible with codebases that prefer/require their contributors to declare the return type of their functions (e.g. through eslint)

Another workaround exists by using satisfies on the entire function:

const fn = (() => { /* ... */ }) satisfies () => AcceptableType

playground link

but having to parenthesize the entire function just to be able to operate on it as an expression and then include the function signature in the satisfies type is at best impractical and at worst not even an option, in the case of function fn() { /* ... */ } declarations. Of course, function declarations will be available as expressions after the declaration, at which point satisfies can be used, but not to its full extent because the type-inference will no longer be able to guide the implementation of the function declaration from within the function body

// this correctly errors but...
getResult satisfies () => { a: string, b: number }

function getResult() {
  return {
    // you don't get any type-hints or type-checking here
  }
}

playground link

Being able to specify that the return type of a function satisfies a type would resolve all of the above drawbacks

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

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

はじめの一歩

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

調査の方向性

リポジトリのファイルやテストは挙げられていません。まず、既存の satisfies 演算子の動作を、リンクされた Playground の例および提案されている戻り値型の構文と比較してください。関数の戻り値を検査しつつ狭い推論を維持し、関数式と関数宣言の両方で機能する、合意された実装ができれば完了です。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
28/100

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

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