microsoft / microsoft/TypeScript

Incomplete infer for generic types

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

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

Bug Domain: check: Type Inference
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

TypeScript Version: 4.0.5

Search Terms:
typescript, infer, incomplete, unknown, generic

Expected behavior:
The compiler should be able to extract information of the generic Item type from the function definition and infer string.

Actual behavior:
The compiler is able to infer the Names type as { id: string } but the Item type is unknown.

Related Issues:

Code

interface LoaderConfig<Key, Item> {
  loadItems: (keys: readonly Key[]) => Promise<readonly Item[]>;
  getKey: (item: Item) => Key;
  makeCacheKey: (key: Key) => string;
}

type LoaderConfigByName<Names, Item> = {
  [name in keyof Names]: LoaderConfig<Names[name], Item>;
};

function makeLoader<Names, Item>(
  configs: LoaderConfigByName<Names, Item>
): void {
  console.log(configs);
}

makeLoader({
  id: {
    loadItems: async (keys: readonly string[]): Promise<readonly string[]> => {
      return await Promise.resolve(keys);
    },
    getKey: (item: string): string => item,
    makeCacheKey: (key: string): string => {
      return key;
    }
  }
});
Output
"use strict";
function makeLoader(configs) {
    console.log(configs);
}
makeLoader({
    id: {
        loadItems: async (keys) => {
            return await Promise.resolve(keys);
        },
        getKey: (item) => item,
        makeCacheKey: (key) => {
            return key;
        }
    }
});

Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

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

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

はじめの一歩

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

調査の方向性

提供された Playground リンクと、TypeScript 4.0.5 を使用したジェネリックな makeLoader の例から始めてください。推論結果を再現し、コンパイラーがジェネリック型 Item をどのように処理するかを追跡してください。例が Item を string として推論し、推論された Names 型を保持できれば完了です。

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

評価

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

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

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