Skip to content
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

Partials with extends works incorrectly with object literals #17491

Closed
cevek opened this issue Jul 28, 2017 · 7 comments
Closed

Partials with extends works incorrectly with object literals #17491

cevek opened this issue Jul 28, 2017 · 7 comments
Labels
Duplicate An existing issue was already created

Comments

@cevek
Copy link

cevek commented Jul 28, 2017

TypeScript Version: 2.5.0 nightly

Code

type X = {a: number};
const y:X = {a: 1, b: 2}; // it's ok - errored

function foo<Super, Part extends Super>(superObj: Partial<Super>, partObj: Partial<Part>) {}
foo({foo: 1, bar: '2'}, {foo: 1,bar: ''}); // it's ok
foo({foo: 1, bar: '2'}, {foo: 1}); // it's ok
foo({foo: 1, bar: '2'}, {foo: 1, Bar: ''}); // it's ok - errored Bar unknown prop
foo({foo: 1, bar: '2'}, {foo: 1, bar: '', baz: ''}); // bad - no error
@RyanCavanaugh
Copy link
Member

This does what you want

function foo<Super>(superObj: Partial<Super>, partObj: Partial<Super & {}>) { }
foo({ foo: 1, bar: '2' }, { foo: 1, bar: '' }); // it's ok
foo({ foo: 1, bar: '2' }, { foo: 1 }); // it's ok
foo({ foo: 1, bar: '2' }, { foo: 1, Bar: '' }); // errors
foo({ foo: 1, bar: '2' }, { foo: 1, bar: '', baz: '' }); // errors

See also #14829

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 28, 2017
@cevek
Copy link
Author

cevek commented Jul 28, 2017

Also doesn't work for Records

function foo<Super, Part extends Partial<Record<keyof Super, any>>>(superObj: Super, partObj: Part) {}

foo({foo: 'foo', bar: 'bar'}, {foo: 1, bar: 1, sdf: 1}); // no error but should be

@cevek
Copy link
Author

cevek commented Jul 28, 2017

@RyanCavanaugh How can I return partObj type from function?

function foo<Super>(superObj: Partial<Super>, partObj: Partial<Super & {}>) { return partObj }
foo({ foo: 1, bar: '2' }, { foo: 1 }).bar // no errored

@RyanCavanaugh
Copy link
Member

It would be helpful to understand what you want the semantics of the function to be.

@cevek
Copy link
Author

cevek commented Jul 28, 2017

@RyanCavanaugh
I need some validator for my graphql functions which can validate right object fields to contain only Class interface fields

interface Class<T> {
    new (): T;
}
class Foo {
    foo: number;
    bar: string;
}
function validate<Super, Args>(Cls: Partial<Class<Super>>, partObj: Partial<Super & {}>, args: Args) { 
    return {...partObj, __class: Cls, __args: args} 
}

function fetchGraphQL<T>(scheme: T): Promise<T> {}
fetchGraphQL(validate(Foo, { foo: 1 }, {arg: 1})).then(obj => obj.foo);

@cevek
Copy link
Author

cevek commented Jul 28, 2017

This case works, but without fields autocomplete

function validate<Super extends Part, Part, Args>(Cls: Class<Super>, partObj: Part, args: Args): Part {}

@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.

@mhegazy mhegazy closed this as completed Aug 17, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants