microsoft / microsoft/TypeScript

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

Offen
#28,788 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die vorgeschlagenen generischen TypeSection-Beispiele und den verlinkten Issue #28787 zu inferierten generischen Typen aus der Record-Aggregation zu lesen. Es sind keine Implementierungsdateien oder Tests genannt; abgeschlossen wäre die Aufgabe erst mit einem geklärten Design, einem vereinbarten Umfang und dem entsprechenden Compilerverhalten samt Tests.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
20/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.