microsoft / microsoft/TypeScript
Can't dynamically map type from class private field
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
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 }));
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con gli esempi TypeScript funzionanti e non funzionanti presenti nell’issue, concentrandoti su come keyof e indexed access trattano il campo privato #events. Determina il comportamento previsto di mapped-type per i campi privati e convalidalo con entrambi gli esempi. Il lavoro è completato quando l’esempio con il campo privato inferisce i tipi del payload degli eventi come mostrato, senza modificare il comportamento a runtime di JavaScript.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100