Skip to content
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

Support assignment in typeguard if block #1769

Closed
fdecampredon opened this issue Jan 22, 2015 · 4 comments
Closed

Support assignment in typeguard if block #1769

fdecampredon opened this issue Jan 22, 2015 · 4 comments
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@fdecampredon
Copy link

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.

@fdecampredon fdecampredon changed the title Support assignment in typeguar if block Support assignment in typeguard if block Jan 22, 2015
@danquirk danquirk added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Jan 22, 2015
@Deathspike
Copy link

I encountered the same thing. This would be a great addition to avoid unnecessary rewrites when adopting TS.

@falsandtru
Copy link
Contributor

+1

@ivogabe
Copy link
Contributor

ivogabe commented Feb 7, 2016

I've opened a PR at #6959.

@RyanCavanaugh
Copy link
Member

Works now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants