microsoft / microsoft/TypeScript

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

Open
#28,788 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the proposed generic TypeSection examples and the linked issue #28787 on inferred generic types from Record aggregation. No implementation files or tests are named; done would require a clarified design, an agreed scope, and corresponding compiler behavior and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.