Skip to content

Narrowing with conditional types #57707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
malthe opened this issue Mar 9, 2024 · 2 comments
Closed

Narrowing with conditional types #57707

malthe opened this issue Mar 9, 2024 · 2 comments

Comments

@malthe
Copy link

malthe commented Mar 9, 2024

πŸ”Ž Search Terms

"extends narrowing", "existential types"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAYg9nAPAaQDRQCoD4oF4rJQQAewEAdgCYDOUA1hCHAGaZQD8mA2sgLpQAuKOQgA3CACcA3ACgZzAK7kAxsACWcclDLVgARhTpsACmYIh8JGkxYAlFADeUAL5zFK9Zu0RdAJhREpBQ09IwsmEZYpubcfPYOMlBJ3rp60XC2sq5AA

πŸ’» Code

type Foo<K, T> = K extends keyof T ? T[K] : never;

function test1<K, T>(foo: Foo<K, T>) { }

function test2<K extends keyof T, T>(foo: T[K]) {
    test1(foo);
    //    ^ Argument of type 'T[K]' is not assignable to parameter of type 'Foo<K, T>'
}

πŸ™ Actual behavior

We get an error message:

Argument of type 'T[K]' is not assignable to parameter of type 'Foo<K, T>'

πŸ™‚ Expected behavior

No error.

Additional information about the issue

I have tried asking this as a question on StackOverflow, but haven't received an answer that resolves whether this is an expected behavior or a current limitation of the compiler narrowing capabilities.

The problem came up during the rather complex typing for adding generics to the EventEmitter class.

interface T {
    myEvent: [string]
}

// It's now possible to have a specialized event emitter:
const emitter: EventEmitter<T> = new EventEmitter();

// We expect to be able to type the listener a straight-forward way:
type Listener<K extends keyof T> = (...args: T[K]) => void;

function on<K extends keyof T>(event: K, listener: Listener<K>): void {
    // This won't work because `on` is typed using conditional typing:
    emitter.on(event, listener);
}

The code example presented in this issue is a distillation of this issue where knowing that K extends keyof T is not enough to narrow a conditional type with the exact same condition.

The following typing fixes the error:

// Yuck!
type Listener<K> = K extends keyof T ? (
        (...args: T[K]) => void
    )
    : never;

Finally, if you're wondering why the typing of EventEmitter uses conditional typing in this way, the answer is because it has to support the default case with all the bells and whistles (i.e. how it's been used in existing code).

@MartinJohns
Copy link
Contributor

MartinJohns commented Mar 9, 2024

The error in your emitter example is correct, because K can be a union type, so event and listener can have incompatible types. Same issue as #57691 and many many others.

The first error is most likely simply due to the deferred resolving of conditional types when they involve an unbound generic type. The compiler has no idea what the types K or T are, so it can't know what type Foo<K, T> resolves to. A known design limitation.

@malthe
Copy link
Author

malthe commented Mar 10, 2024

Closing because this needs #27808 (a oneof qualifier).

@malthe malthe closed this as completed Mar 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants