microsoft / microsoft/TypeScript

TypeScript incorrectly says return is not assignable to an intersection containing all the required props

オープン
#43,366 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

Bug Domain: Intersection
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Bug Report

🔎 Search Terms

& generic intersection omit spread

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics
⏯ Playground Link

Playground link with relevant code

💻 Code
export interface BaseProps {
  id?: string;
  name: string;
}

export type PropsWithId<P extends BaseProps> = Omit<
  P,
  'id'
> & { id: string };

export default function usePropsWithId<P extends BaseProps>(
  props: P
): PropsWithId<P> {
  const { id: idProp, name, ...other } = props;
  const id = idProp ?? `${name}-id`;

  return { id, name, ...other };
}
🙁 Actual behavior

The return line gets this TypeScript error:

Type '{ id: string; name: string; } & Omit<P, "id" | "name">' is not assignable to type 'PropsWithId<P>'.
  Type '{ id: string; name: string; } & Omit<P, "id" | "name">' is not assignable to type 'Omit<P, "id">'.

This doesn't make sense since PropsWithId<P> is Omit<P, 'id'> & { id: string };

🙂 Expected behavior

This should work without a TypeScript error.

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

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

はじめの一歩

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

調査の方向性

リンクされた TypeScript Playground と issue 内のジェネリックな交差型の例から始め、次に checker が返されたオブジェクトを PropsWithId

に対してどのように評価するかを追跡します。issue ではリポジトリのファイルやテストは指定されていません。完了条件は、報告された assignability エラーなしでサンプルがコンパイルされ、期待される PropsWithId

型が維持されることです。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

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

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