microsoft / microsoft/TypeScript

Can't dynamically map type from class private field

Open
#44,633 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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 }));

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 with the working and non-working TypeScript examples in the issue, focusing on how keyof and indexed access treat the private #events field. Determine the intended mapped-type behavior for private fields and validate it against both examples. Done means the private-field example infers the event payload types as shown without changing JavaScript runtime behavior.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.