microsoft / microsoft/TypeScript

Infer Generics Types, from Record<string<...> Aggregation for return type constraints

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

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

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

説明

If you can imagine capturing type information at the same time as runtime information, were the inferred types literals match that those of the runtime types, so that one can implicitly at the same time
capture the typescript types and runtime information for the shape of the typescript type, in one shot.
This I have been doing quite successfully, and is quite powerfully. I find myself using what I term type constraints, to capture information at runtime in specific categories, that typings constraints impose. Which allows me to do advance record mixing.
This allows one to impose constraints on free form runtime information, to ensure
runtime information is correctly categorized for typing system performance of mixing.

Example of return type constraints for categorization for mixing

interface IConstraints<Param1 extends 'Param1A' | 'Param1B',
Param2 extends 'Param2A' | 'Param2B',
Param3 extends 'Param3A' | 'Param3B'> = {
__Param1 : Param1
__Param2 : Param2
__Param3 : Param3
}
// Typically Record< IConstraints.. Is a recursive record capture pattern, with neasted records
class RecordMix<
Dim1 extends Record<string, IConstraints<'Param1A',any,any>>,
Dim2 extends Record<string, IConstraints<'Param1A',any,any>,
Dim3 extends Record<string, IConstraints<never, 'Param2B','Param3A'>
Dim4 extends Record<string, IConstraints<never,'Param2A','Param3B'>
{
 // Use the constructor to capture the runtime sets of information.
}

Functionality as of current.

interface Builder<A extends string, B extends string>
{
    __A : A,
    __B : B
}

type AString = 'A' | 'E' | 'G';
type BString = 'B' | 'F' | 'H';

type RecordBuilder<A extends AString, B extends BString> = Record<string, Builder<A,B>>

const builderRecord = {
    a : {} as any as Builder<'A','B'>,
    e : {} as any as Builder<'E','F'>,
   // g : {} as any as Builder<'G','F'>,
}

const builderRecordInvalid = {
    a : {} as any as Builder<'A','B'>,
    e : {} as any as Builder<'E','F'>,
   g : {} as any as Builder<'G','F'>,
}

function Capture<A extends 'A' |'E', B extends BString, Rec extends RecordBuilder<A, B>>(record : Rec)
{
    return {} as any as {Ra:A,Rb:B};
}

const result = {a:Capture(builderRecord)} //OK
const result = {a:Capture(builderRecordInvalid)} //Failed input type constraints not met

// Type
const result: {
    a: {
        Ra: "A" | "E";
        Rb: BString;
    };
}

Functionality Feature Request, using infer of aggregate


function Capture<A extends 'A' |'E', B extends BString, Rec extends RecordBuilder<infer A, infer B>>(record : Rec)
{
    return {} as any as {Ra:A,Rb:B};
}

const result = {a:Capture(builderRecord)} //OK
const result = {a:Capture(builderRecordInvalid)} //Failed input type constraints not met

// Type Results
const result: {
    a: {
        Ra: "A" | "E";
        Rb: "B" | "F";
    };
}

Functionality Feature Request current mechanisms


type ExtractRecordComponent<Shape extends Record<string,any>, T extends Record<string, Shape>, Key extends string> = ({
    [K in keyof T] : T[K][Key]
})[keyof T]

function Capture<A extends 'A' |'E', B extends BString, Rec extends RecordBuilder<A, B>>(record : Rec)
 : {Ra:ExtractRecordComponent<Builder<any,any>,Rec,'__A'>, Rb:ExtractRecordComponent<Builder<any,any>,Rec,'__B'>}
{
    return {} as any;
}

const result = {a:Capture(builderRecord)} //OK
const result = {a:Capture(builderRecordInvalid)} //Failed input type constraints not met

// Type Results
const result: {
    a: {
        Ra: "A" | "E";
        Rb: "B" | "F";
    };
}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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

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

はじめの一歩

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

調査の方向性

この Issue では、ソースファイル、テスト、エントリポイントが指定されていません。まず RecordBuilder と Capture の例を最小限の推論ケースに縮小し、次に要求されている aggregate infer の動作を現在の ExtractRecordComponent workaround と比較します。完了条件は、記載された受け入れ対象および拒否対象の入力に対する、正確な推論ルールとテストが用意されていることです。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
20/100

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

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