Skip to content

[Feature Request] Type guard on conditional types #23493

Closed
@Cryrivers

Description

Code

type Test<V, M extends boolean> = { multiple?: M } & (
    M extends true ? { values: V[] } : { value: V }
)

function typeGuard<V, M extends boolean>(input: Test<V, M>) {
    if (input.multiple) {
        return input.values;
    } else {
        return input.value;
    }
}

Expected behavior:
input.values can be inferred in if block and input.value can be inferred in else block.

Actual behavior:
Property 'values' does not exist on type 'Test<V, M>'.
Property 'value' does not exist on type 'Test<V, M>'.

Playground Link: https://www.typescriptlang.org/play/#src=type%20Test%3CV%2C%20M%20extends%20boolean%3E%20%3D%20%7B%20multiple%3F%3A%20M%20%7D%20%26%20(%0D%0A%20%20%20%20M%20extends%20true%20%3F%20%7B%20values%3A%20V%5B%5D%20%7D%20%3A%20%7B%20value%3A%20V%20%7D%0D%0A)%0D%0A%0D%0Afunction%20typeGuard%3CV%2C%20M%20extends%20boolean%3E(input%3A%20Test%3CV%2C%20M%3E)%20%7B%0D%0A%20%20%20%20if%20(input.multiple)%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20input.values%3B%0D%0A%20%20%20%20%7D%20else%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20input.value%3B%0D%0A%20%20%20%20%7D%0D%0A%7D

Related Issues:

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions