microsoft / microsoft/TypeScript

TypeScript 4.1+: Generic binding too broad in recursive conditional types

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

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

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

説明

TypeScript Version: 4.2.0-dev.20201103

Search Terms: Recursive Conditional types, generics, tuple types

Code

// A test object
const obj = {
    a: '2',
    b: {
        c: 3,
        d: {
            e: 'string',
            f: {
                g: {
                    h: 4
                }
            }
        }
    }
} as const

// its type
type Struct = typeof obj;

/**
 * First recursive conditional type 
 * 
 * CheckArguments gets 
 * Obj - A nested object
 * Arugments - A tuple of keys to go down a nested path
 * 
 * The type is recursive, I check if the current argument lists extends keyof Obj --> Then the recursion ends
 * Otherwise, I check if the first value in the tuple is keyof Obj, infer the rest, and go down the same type
 * again
 */
type CheckArguments<Obj, Arguments> = 
    Arguments extends [keyof Obj] ? Obj[Arguments[number]] :
    Arguments extends [keyof Obj, ...infer U] ? CheckArguments<Exclude<Obj[keyof Obj], string | number>, U> : never;

/**
 * Tests, all 👍
 */
type Foo = CheckArguments<Struct, ['a']> // "2"
type Foo2 = CheckArguments<Struct, ['b', 'd', 'e']> // "string"
type Foo3 = CheckArguments<Struct, ['b', 'd', 'f', 'g', 'h']> // 4

/**
 * Second recursive conditional type
 * 
 * Arguments gets
 * Obj - A nested Obj
 * 
 * The recursive conditional type creates a union type of possible nested arguments in a tuple
 * This is based on Anders example from TSConf: https://github.com/ahejlsberg/tsconf2020-demos/blob/master/template/main.ts
 * (Dotted paths)
 */
type Arguments<Obj> = 
    Obj extends object ?
        [keyof Obj] | SubArguments<Obj, keyof Obj> :
        never;

// A helper type
type SubArguments<Obj, Key> =  Key extends keyof Obj ? [Key, ...Arguments<Obj[Key]>] : never;

// For example, the possible tuples of Struct 👍
type Bar = Arguments<Struct>;

// equals to this union type
type Bar2 = ["a" | "b"] | ["b", "c" | "d"] | ["b", "d", "e" | "f"] | ["b", "d", "f", "g"] | ["b", "d", "f", "g", "h"]

/**
 * So both recursive conditional types work on their own. A problem is once I want to combine
 * them in a function, where I expect the first argument to bind to a value type within the Arguments union
 * 
 * Instead of having just one value type passed to CheckArguments (the one that is bound through the generic Keys),
 * TypeScript passes all parts of the union to CheckArguments. This leads CheckArguments to return all possible values
 */

declare function get<Obj extends object, Keys extends Arguments<Obj>>(o: Obj, ...keys: Keys): CheckArguments<Obj, Keys>

/** 
 * Tests 💥
 * */
const foo = get(obj, 'a') // Should be "2" 😢 
const foo1 = get(obj, 'b', 'c') // Should be  3 😢 
const foo2 = get(obj, 'b', 'd', 'e') // Should be "string" 😢 
const foo3 = get(obj, 'b', 'd', 'f', 'g', 'h') // Should be 4 😢 

Expected behavior: Keys gets bound to the value type passed as an argument to the function. This value type is then used for CheckArguments

Actual behavior: Keys is the entire union type Arguments<Obj>, not the subset. This leads to CheckArguments returning a too broad return type (and taking very long to evaluate ;-))

Playground Link: Click here

Related Issues: Did not find any.

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

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

はじめの一歩

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

調査の方向性

まず、報告されたバージョン 4.2.0-dev.20201103 を使用して、リンク先の TypeScript Playground でジェネリック推論のケースを再現し、foo から foo3 までの推論された型を期待される結果と比較します。各呼び出しで Keys が Arguments の完全なユニオンではなく、指定されたタプルにバインドされ、同時に説明されている過剰な評価を回避できれば完了です。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

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

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