microsoft / microsoft/TypeScript

Generic TypeSection for specialization of nested Record<string,.. Capturing... mechanics

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

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

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

説明

The ability to define a typeSection, were one can use generics to narrow and customize the functions and statics for record structures.

A union syntax for the Record<string,string | TypeSectionName>
and returnType function() : string |TypeSectionName

Would be used to more specialize the typescript inside a typesections

typeSection TypeSectionName<Gen1 extends 'Gen1A' | 'Gen1B', Gen2 extends 'Gen2A' | 'Gen2B'>

// whole bunch of functions and statics, which can't be customized, narrow more spesifically
// from a generic pattern to more spesifics, when capturing runtime information in shapes.

function ArrayItems<Param1 extends string, Gen1Mod extends Gen1,
Gen2Mod extends Gen2, ReturnTypeResult extends GenBuilder<ArrayItems,Gen1Mod, Gen2Mod>>(arrayItems : Record<string, infer Param1>, gen1 : infer Gen1Mod, gen2 : infer Gen2Mod) : 
ReturnTypeResult
{
    return new ArrayItems(arrayItems, gen1 : gen2);
}

typeSectionEnd

type GenBuilderNarrowMap<Gen1 extends 'Gen1A' | 'Gen1B', Gen2 extends 'Gen2A' | 'Gen2B'> = 
{
    'Gen1A' : 'Required'
    'Gen1B' : 'Optional'
}[Gen1]
|
{
    'Gen2A' : 'Nullable'
    'Gen2B' : never
}[Gen2]

type GenBuilder<
Base extends GenBuilder<any,any>,
Gen1 extends 'Gen1A' | 'Gen1B', Gen2 extends 'Gen2A' | 'Gen2B',
Keys = GenBuilderNarrowMap<Gen1, Gen2>>
= Pick<Base<Gen1Mod, Gen2Mod>, Keys>  & Remove<Base<Gen1,Gen2>, Keys>

interface GenBuilder<Gen1Mod extends Gen1, Gen2Mod extends Gen2>
{
    Required() : any
    Optional() : any
    Nullable() : any
}

class ArrayItems<Gen1 extends 'Gen1A' | 'Gen1B', Gen2 extends 'Gen2A' | 'Gen2B'> 
extends GenBuilder<Gen1,Gen2>
{
    constructor(public config : {Required:boolean} )
    {}

    // Where the typesection woul futher mutate and constraint the type section by narrow it, for 
    // any instance mentiond after the use of required.
    Required<ReturnTypeResults extends GenBuilde<Remove<Gen1,'Gen1A'>,Gen2>() : TypeSectionName<Ge1,Gen2>,ReturnTypeResults // Here return same structure, using the builder pattern to futher constrain the avaliable
    {
        this.Required = false;
        new Arrayitems<'Gen1A', Gen2>(this);
    }

    Optional() : ArrayItems<'Gen2B',Gen2>

    Nullable() : ArrayItems<Gen1, 'Gen2A'>

    ArraySpesific() : number;
}


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
interface RecordContraints<Gen1 extends 'Gen1A' | 'Gen1B', Gen2 extends 'Gen2A' | 'Gen2B'>
// TypeSectionName means that for the next inner neasted evaluations of the record patterns,
// all those type signatures, will have become more spesification by the TypSectionName Generic Specialization.
extends Record<string, TypeSectionName<Ge1,Gen2>, (RecordContraints<Gen1, Gen2>**ByNeastedFunctionInputOtherwise** | IConstraints<Gen1,Gen2,any>)
{
 // Use the constructor to capture the runtime sets of information.
}

class Categorize<
Cat1 extends RecordContraints<'Gen1A',any>,
Cat2 extends RecordContraints<'Gen1B','Gen2A'>,
Cat3 extends RecordContraints<'Gen1B','Gen2B'>>
{
    constructor(public Cat1 : Cat1, public Cat2 : Cat2, public Cat3 : Cat3)
    {
    }
// All of this builds up to the following to the following, which allows
// mixing of types, to produce the correct well form record structure for typcially 8/16 different set, permutations.
// To ensure a typescript project is fully type checked.
    newRecord(rec : Ca1 & Cat2) : void
    updateRecord(rec : Partial<Cat2> & Cat3) : void
    results() : Required<Ca1> & Partial<Cat2> & Cat3
}


const category = new Categorize(
    {
        // These are now specialize based on this sections intanse of the generics.
        ArrayItems({})
    },
    {
        // These are now specialize based on this sections intanse of the generics.
        ArrayItems({})
    },
    {
        ArrayItems({})
    }
)

See Infer Generics Types, from Record<string<...> Aggregation for return type constraints on how both features work to get a bigger picture
https://github.com/Microsoft/TypeScript/issues/28787

  • 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 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提案されているジェネリックな TypeSection の例と、Record の集約から推論されるジェネリック型に関するリンク先の issue #28787 を読みます。実装ファイルやテストは指定されていません。完了とするには、設計の明確化、合意されたスコープ、それに対応する compiler の動作とテストが必要です。

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

評価

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

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

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