microsoft / microsoft/TypeScript
Can't dynamically map type from class private field
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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 }));
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con los ejemplos de TypeScript que funcionan y los que no funcionan en el issue, centrándote en cómo keyof y indexed access tratan el campo privado #events. Determina el comportamiento previsto de mapped-type para los campos privados y valídalo con ambos ejemplos. Se considera terminado cuando el ejemplo del campo privado infiere los tipos de payload de los eventos tal como se muestran, sin cambiar el comportamiento en tiempo de ejecución de JavaScript.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100