-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Conditional type is not resolved when used in generics #51080
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
Comments
Resolving of conditional types including unbound type arguments is deferred. This is a design limitation. Your example would not be type-safe either way, as the type |
What do you mean by |
interface Example { hello: 'abc', none: never }
const imp = new Implementation<Example>() According to the provided type, the type of the property |
But there's no string literal in the playground example or in the original post, there's only this: interface PayloadMap { hello: string, none: never }
let impl = new Implementation<{test: string} & PayloadMap>(); The payload map is extended. Anyway - the main problem is that the type system has a limitation and cannot resolve this? Can it be implemented differently? I just want to avoid having 2 overloads for the emit method as that would mean that events with payloads can be emitted without it (which is something we don't want). This is what I want to avoid: emit<K extends keyof T>(type: K): Base<T>;
emit<K extends keyof T>(type: K, payload?: T[K]): Base<T> {
// emit event
} Because this call should be invalid: impl.emit('hello'); |
Extending Your second example works, because the type argument is not unbound anymore at the point where you call |
Omg, thanks! I didn't realize that you are extending the original type, now I can see where is the problem. Guess I can close the issue now. |
Bug Report
π Search Terms
conditional types, generics
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
This code example doesn't compile and I think it should as the generic is always constraint to
PayloadMap
.π Expected behavior
The example has compile errors because compiler is unable to find out that T is always compatible with
PayloadMap
.Some context for the issue I'm dealing with - I'm trying to change typings to a base class we have in our library that is responsible for emitting events and acts as a base class for all other objects that need to emit events in the lib. We have an interface with
eventName: PayloadType
mapping - it basically defines all events that we emit and what payloads they have. This map is currently static but we want to make it generic so that users of the library can extend it and emit custom events. We need to support events with no payload - those are represneted by thenever
type. If the payload is never,emit
function should be callable only with one argument, if the payload is any other type, the emit function has to be called with 2 arguments - that's what thePayload
type does (and it works well - until we introduce generics).I was looking at this bug report as well but it seems to be about something different, but I'm not 100% sure - #33369.
The text was updated successfully, but these errors were encountered: