microsoft / microsoft/TypeScript

Accumulating type in TypeScript like powerful instrument for organizing code

Offen
#36,625 4 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Main Idea

Accumulating type is a type that combines the types of declarations that occur in different parts of the code with the same name. Type merging is done according to the OR principle.
Principles and dogmas:

  • related code in one place
  • organizing separate and independent modules
  • write once use everywhere
  • intermodular interaction through types

Search Terms

accumulate type, type system

Suggestion

Add the ability to declare accumulative types

Examples

// file: moduleAFolder/moduleA.ts
    accumulate type ModuleNames = "ModuleA"; // type to be "ModuleA" | "ModuleB"
    
// file: moduleAFolder/moduleAHelper.ts
    import {ModuleNames} from "moduleAFolder/moduleA";
    //      ^ powerful part of example: importing type ONLY from moduleA, NOT from ModuleB
    //      ^ accessible value for ModuleNames here are "ModuleA" and "ModuleB"

    const a: ModuleNames = "ModuleA"; // it's ok
    const b: ModuleNames = "ModuleB"; // it's ok.
    const c: ModuleNames = "Unknown"; // it's error
    
// file: moduleBFolder/moduleB.ts
    accumulate type ModuleNames = "ModuleB"; // type to be "ModuleA" | "ModuleB";                   

Example above is very simple but illustrates all principles good code. In more difficalt cases we have 100 modules and 1000 types. Some of them has types that need in another modules.

Yes, we can create folder with name 'shared' and move common types to this folder:

  • but then we destroy the integrity of the code inside the separate module. Everything that looked whole before that became fragmented;
  • but then we make the “shared” folder a trash or garbage;

Yes, we can copy common types from one module to another:

  • but then we make smelling bad code;
  • but then we make bug prone code;

More difficult example

 // file: moduleAFolder/moduleA.ts
     accumulate type CrossModuleTypes = {
         "ModuleA": {
             id: string;
             data: {
                 value: string;
             }
         }
     }
     
     
 // file: moduleBFolder/moduleB.ts
     accumulate type CrossModuleTypes = {
         "ModuleB": {
             id: string;
             segment: {
                 part: string;
             }
         }
     }
     
 // file: moduleCFolder/moduleC.ts
     // no imports here!
     accumulate type CrossModuleTypes; // nothing merge, only usaging;
     type UnionToIntersection<U> =  (U extends any ? (k: U)=>void : never) extends ((k: infer I)=>void) ? I : never
     type ModuleBType = UnionToIntersection<CrossModuleTypes>["ModuleB"];
     const cachingData: ModuleBType = this.loadFromCache("ModuleB");
     console.log("Caching segment from ModuleB is ", cachingData.segment.part); // no errors here.

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.

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

Beginnen Sie mit der Durchsicht der vorgeschlagenen Syntax und der im Issue verlinkten TypeScript Design Goals. Es sind keine Implementierungsdateien, Tests oder Compiler-Einstiegspunkte angegeben, daher besteht die erste Aufgabe darin, die Bereiche des Typprüfers und der Modulauflösung zu lokalisieren, die Deklarationen über Module hinweg steuern. Als abgeschlossen gilt die Definition und Validierung akkumulativer Typs semantik unter Beibehaltung der bestehenden JavaScript-Ausgabe.

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
25/100

Neue Issues direkt in Ihr Postfach

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