Skip to content

Partial does not work well with generic guards #18072

Closed
@RReverser

Description

@RReverser

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions