Skip to content

Partial generic types get resolved completely incorrectlyΒ #63558

@Mudloop

Description

@Mudloop

πŸ”Ž Search Terms

incorrect generic resolution

πŸ•— Version & Regression Information

I don't think this is caused by a new update.

⏯ Playground Link

https://tsplay.dev/weG9Xm

πŸ’» Code

export interface Item1 { kind: "item1" }
export interface Item2 { kind: "item2" }
export interface Item3 { kind: 'item3' }
export type Item = Item1 | Item2

const Guard1 = (o: unknown): o is Item => true
const Guard2 = <M extends Item>(o: M | Item3): o is M => true
export function Test1<M extends Item>(src: M | Item3) {
    if (!Guard1(src)) {
        // these are flipped :
        src					// Item3 | M
        src.kind            // item3
    }

    if (!Guard2(src)) {
        // and so are these :
        src					// Item3
        src.kind            // "item1" | "item2" | "item3"

        // And src isn't even typeof src :
        const test: typeof src = src;
        // Type 'Item1 | Item2 | Item3' is not assignable to type 'Item3'.
    }
}

πŸ™ Actual behavior

Looks like there's two resolvers that come up with opposing results, and both are used.

πŸ™‚ Expected behavior

Consistent type resolution.
Edit: or potentially an error message to indicate it's not resolvable.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions