microsoft / microsoft/TypeScript
Can't dynamically map type from class private field
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
Suggestion
Support private fields in mapped types.
🔍 Search Terms
typescript, private field, mapped types
✅ Viability 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
Include information about private fields types inside class interface.
📃 Motivating Example
TypeScript have dynamic mapped types which is very powerful feature. But currently can't used with private fields. So its hard to migrate to those.
💻 Use Cases
Working example without private field:
interface Runnable {
(): void;
}
interface Consumer<T> {
(data: T): void;
}
class EventEmitter<T> {
emit(data: T): void {
return void 0;
}
subscribe(consumer: Consumer<T>): Runnable {
const unsubscribe = () => void 0;
return unsubscribe;
}
}
class Host {
private readonly events = {
init: new EventEmitter<Record<string, number>>(),
contextUpdate: new EventEmitter<Record<string, string>>(),
inactivity: new EventEmitter<boolean>(),
} as const;
on<T extends HostEventType>(
type: T,
consumer: HostEventConsumer<T>,
): Runnable {
return this.events[type].subscribe(consumer);
}
}
type HostEventType = keyof Host['events'];
type HostEventConsumer<T extends HostEventType> =
Host['events'][T] extends EventEmitter<infer U> ? Consumer<U> : never;
const host = new Host();
// Here data is mapped to Record<string, number>
host.on('init', (data) => console.log({ data }));
Not working example with private field (expecting to work in future):
interface Runnable {
(): void;
}
interface Consumer<T> {
(data: T): void;
}
class EventEmitter<T> {
emit(data: T): void {
return void 0;
}
subscribe(consumer: Consumer<T>): Runnable {
const unsubscribe = () => void 0;
return unsubscribe;
}
}
class Host {
readonly #events = {
init: new EventEmitter<Record<string, number>>(),
contextUpdate: new EventEmitter<Record<string, string>>(),
inactivity: new EventEmitter<boolean>(),
} as const;
on<T extends HostEventType>(
type: T,
consumer: HostEventConsumer<T>,
): Runnable {
return this.#events[type].subscribe(consumer);
}
}
type HostEventType = keyof Host['#events'];
type HostEventConsumer<T extends HostEventType> =
Host['#events'][T] extends EventEmitter<infer U> ? Consumer<U> : never;
const host = new Host();
// Here data should be mapped to Record<string, number>
host.on('init', (data) => console.log({ data }));
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit den funktionierenden und nicht funktionierenden TypeScript-Beispielen im Issue und konzentriere dich darauf, wie keyof und indexed access das private #events-Feld behandeln. Ermittle das beabsichtigte mapped-type-Verhalten für private Felder und validiere es anhand beider Beispiele. Als abgeschlossen gilt die Aufgabe, wenn das Beispiel mit dem privaten Feld die Event-Payload-Typen wie gezeigt ableitet, ohne das Laufzeitverhalten von JavaScript zu ändern.
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
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100