microsoft / microsoft/TypeScript

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

Ouverte
#28,788 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par lire les exemples proposés de TypeSection générique et l’issue liée #28787 sur les types génériques inférés à partir de l’agrégation de Record. Aucun fichier d’implémentation ni test n’est indiqué ; pour considérer le travail terminé, il faudrait une conception clarifiée, un périmètre convenu, ainsi que le comportement correspondant du compilateur et les tests associés.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
20/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.