microsoft / microsoft/TypeScript

Incorrect conditional type evaluation on transformed function type expressions

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

@weswigham がすでに取り組んでいます。

2023年9月1日 から。

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

説明

🔎 Search Terms

"conditional type", "function type", "extends", "evaluation", "inference"

🕗 Version & Regression Information

This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.7.4#code/PTAEEFQUwDwFygOwCYGdQCEBQcCeAHKCUAXlAAoA3AQwBsBXKALlFTgCcBLRAcwEpSAPlCUA9p2Q4CRDKQo0GzUAHIAFlFq1RygSWFiJWKYVAAxSGVMAecIKwh5dRizZdeu-eMnGip2ZasMOwcqJyUAFgAmDxEvIwc4dVAAY1EAW3x6BGQRMPRqdiJUjIKoHNT2QuTaPB9QAHkAaTlzaHgkNDNZAH5QDkZQFgAzOlQoAG57MFBQAD1u+LBEolEuHm46NvxC1FROUUR8wtBEUTg6xvqWm2FYBBR0ayDQXv6iYdGJqZm5he+AFVUnHQnAytCgaSQcGocH2iFAyFEUHQpzgbSBACNOGjlqBtqIMeC0nV6lcyABxG5te6dSnPV7sAYfWhjSYOGbzIx4EzWf7UjqPRDCMjkAB04oKPFQLH+7GohyGqzSAAUCtQ0qgrKq5ZCEOxNf9BIIYgAlKBwejsRD-aRWQ1c6SgSl8u4CsxC0hYGba9XmqD6u23doPUDcIb+0A+jX8kP0RAAa1OAHdEABtAC6XpeFHFosl0tAsvlqEV7BVao1WorqGNQlAZotVpthEDWZYiCglH9DpMRYVSqjBuFoAA3qBU81uKB41BcKIhoX0yxmq6QzO5wuM9n-hP04NC7uY501BotMps1F9zvGnuAL6TOqmeFkHMS9hSljy3AZmJxxOiFNxiAA

💻 Code
// A extends B
type A = (value: string) => void
type B = (value: 'hello') => void

type FA = F<A>
// (value: string) => void

type FB = F<B>
// (value: 42) => void

// the computed values are compared correclty
type OK = FA extends FB ? true : false;
// return false

// the original expressions are not
type KO = F<A> extends F<B> ? true : false;
// return true

// This implementation does not exhibit the problem
type OO = G<A> extends G<B> ? true : false;
// return false

type F<T extends Fn> = (...args: TransformParams<Parameters<T>>) => ReturnType<T>

type G<T extends Fn> =
  Parameters<T> extends infer Params extends unknown[]
  ? (...args: TransformParams<Params>) => ReturnType<T>
  : never

type TransformParams<T> = { [K in keyof T]: K extends keyof [] ? T[K] : T[K] extends 'hello' ? 42 : T[K] };

type Fn =  (...args: any[]) => unknown;
🙁 Actual behavior

The comparison of the expressions F<A> and F<B> is incorrect and is not equivalent to the comparison of their results.

🙂 Expected behavior

F<A> extends F<B> should be false because it should evaluate to ((value: string) => void) extends ((value: 42) => void), and string and 42 are unrelated.

Additional information about the issue

Interestingly, G, which should behave the same as F, does not exhibit the problem.

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

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

はじめの一歩

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

評価

この issue はまだ評価されていません。

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

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