-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Partial<generic> fails at matching types #17071
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
This is a correct error. Your definition for test<{foo: "bar" | "qua"}>(); but your implementation clearly fails to return a |
Humm ok I see. Is there a way to work around this that you can think of? |
What's |
Yes, I guess I simplified my example too much. Here is an example closer to what I'm trying to achieve: interface Foo {
foo: boolean;
}
const assign = <T>(o: T, newValues: Partial<T>): T => (
Object.assign({}, o, newValues)
);
const toggleFoo = <T extends Foo>(o: T): T => assign(o, { foo: !o.foo });
// [ts] Argument of type '{ foo: boolean; }' is not assignable to parameter of type 'Partial<T>'.
const bar = {
foo: false,
bar: false,
}
const bar2 = toggleFoo(bar); // bar and bar2 have the same type
console.log(bar2); // { foo: true, bar: false } |
You're going to need a type assertion here: const toggleFoo = <T extends Foo>(o: T): T => assign(o, <T>{ foo: !o.foo });
~~~ |
Ok thanks for the help |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.4.0
Code
Expected behavior: should be ok
Actual behavior:
[ts] Type '{ foo: ""; }' is not assignable to type 'Partial<T>'.
The text was updated successfully, but these errors were encountered: