Skip to content

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

Closed
testerez opened this issue Jul 10, 2017 · 7 comments
Closed

Partial<generic> fails at matching types #17071

testerez opened this issue Jul 10, 2017 · 7 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@testerez
Copy link

TypeScript Version: 2.4.0
Code

interface Foo { foo: string };

function test<T extends Foo>(): Partial<T> {
  return { foo: '' };
}

Expected behavior: should be ok

Actual behavior: [ts] Type '{ foo: ""; }' is not assignable to type 'Partial<T>'.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 10, 2017
@RyanCavanaugh
Copy link
Member

This is a correct error. Your definition for test says it's legal to write this:

test<{foo: "bar" | "qua"}>();

but your implementation clearly fails to return a Partial<{foo: "bar" | "qua"}>

@testerez
Copy link
Author

testerez commented Jul 10, 2017

Humm ok I see. Is there a way to work around this that you can think of?
Because in my implementation I d'ont expect that a subtype of string will be used...

@RyanCavanaugh
Copy link
Member

What's T even doing there? Just write function test(): Partial<Foo>

@testerez
Copy link
Author

testerez commented Jul 10, 2017

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 }​​​​​

@RyanCavanaugh
Copy link
Member

You're going to need a type assertion here:

const toggleFoo = <T extends Foo>(o: T): T => assign(o, <T>{ foo: !o.foo });
                                                        ~~~

@testerez
Copy link
Author

Ok thanks for the help

@mhegazy
Copy link
Contributor

mhegazy commented Aug 17, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants