-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Generic wrapper interface no longer assignable #31804
Comments
The As a workaround (until we take a fix), if you specify type Params<T> = Parameters<Extract<Required<T>, AnyFunction>>; the use of |
It seems like defining Params as you suggest doesn't actually work for this purpose, but I'm not sure why. Simplified example (got rid of the inheritance stuff from B temporarily, better names) type AnyFunction = (...args: any[]) => any;
type Params<T> = Parameters<Extract<T, AnyFunction>>;
interface Caller<T> {
call<K extends keyof T>(event: K, ...args: Params<T[K]>): void;
}
interface ACallable {
foo(thing1: boolean, thing2: number): void;
}
class A {
caller: Caller<ACallable>;
}
new A().caller.call("foo", true, 1); // works (good)
new A().caller.call("foo"); // errors (good) Using your suggested change with type AnyFunction = (...args: any[]) => any;
type Params<T> = Parameters<Extract<Required<T>, AnyFunction>>;
interface Caller<T> {
call<K extends keyof T>(event: K, ...args: Params<T[K]>): void;
}
interface ACallable {
foo(thing1: boolean, thing2: number): void;
bar(): void;
}
class A {
caller: Caller<ACallable>;
}
new A().caller.call("foo", true, 1); // errors, [boolean, number] is not assignable to never
new A().caller.call("bar"); // errors, [] is not assignable to never It does make the inheritance work, but then the thing we're trying to inherit and extend doesn't work anymore, so... ¯\_(ツ)_/¯ |
It seems like I did find an actual workaround tho, and Edit... However, I found the One True Workaround™, and that's just doing the exact same thing that type AnyFunction = (...args: any[]) => any;
type LiterallyJustTheSameThing<T> = { [K in keyof T]: T[K] };
type Params<T> = Extract<LiterallyJustTheSameThing<Parameters<Extract<T, AnyFunction>>>, any[]>;
interface Caller<T> {
call<K extends keyof T> (event: K, ...args: Params<T[K]>): void;
}
interface ACallable {
foo (thing1: boolean, thing2?: number): void;
}
class A {
caller: Caller<ACallable>;
}
interface BCallable extends ACallable {
bar (): void;
}
class B extends A {
caller: Caller<BCallable>;
}
new A().caller.call("foo", true, 1); // works
new B().caller.call("foo", true); // works
new B().caller.call("bar"); // works |
Hopefully you won't need a workaround for long, since #32116 fixes the root cause of the problem. |
TypeScript Version: 3.6.0-dev.20190606
Search Terms:
I didn't know how to search for this issue since it's a bit complex
Code
Expected behavior:
No errors, as it worked in pre 3.5.0
Actual behavior:
Why? What was this used for?
Our project uses a setup like this for strongly typed events; we define the events in interfaces, and then classes that emit the events store the generic event emitter instances on them. (The actual implementation is a lot more complex, this is a stripped down version)
Previously, it worked to simply override the property's type, but now that doesn't work anymore, and I can't find a workaround. I think this is might prevent our team from updating because of how much this breaks, at least for a time... The amount that has to be rewritten to undo the dependency on this functionality is not negligible.
The text was updated successfully, but these errors were encountered: