Skip to content

Partial does not work well with generic guards #18072

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
RReverser opened this issue Aug 27, 2017 · 3 comments
Closed

Partial does not work well with generic guards #18072

RReverser opened this issue Aug 27, 2017 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@RReverser
Copy link
Contributor

TypeScript Version: 2.4.2

Code

(Note this is a simplified example and easy to workaround, but it gets dirtier in a real-world code)

type Obj<B> = { body: B };

function acceptPartial<O extends Obj<string>>(obj: Partial<O>) {}

function acceptFull<O extends Obj<string>>(obj: O) {
    acceptPartial<O>({
        body: obj['body']
    });
}

Expected behavior:

No type errors.

Actual behavior:

temp.ts(8,22): error TS2345: Argument of type '{ body: string; }' is not assignable to parameter of type 'Partial<O>'.

This doesn't make much sense because { body: obj['body'] } is correctly determined as { body: string } which is same as Obj<string>, and so compatible with Partial<Obj<string>> and any Partial<O> in this context.

Moreover, calling acceptPartial<Obj<string>> instead of O works which seems to mean that Partial doesn't currently "understand" that Partial<O> is just a superset of Partial<Obj<string>> when O extends Obj<string>.

@mhegazy mhegazy added the Needs Investigation This issue needs a team member to investigate its status. label Aug 29, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Oct 31, 2017

The core issue here is that this is not allowed:

var p: Partial<O> = { body: "" };

Since the compiler can not verify that body is not specialized in O further.

Duplicate of #19388

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. labels Oct 31, 2017
@RReverser
Copy link
Contributor Author

Well #19388 is newer so that one is technically a duplicate of this :P

@typescript-bot
Copy link
Collaborator

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

@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