Skip to content

Support assignment in typeguard if block #1769

Closed
@fdecampredon

Description

@fdecampredon

it would be nice to support that kind of construction :

function myFunction(arr: string | string[]) {
    if (typeof arr === 'string') {
        arr = [arr]
    }
    //arr is now string[]
}

It's a common pattern used by library that take options in parameter.
Generally you can pass an array of string or a string if you have a single value, and internally the library will create an array in any case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions