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

Type Guards Not Recognised If typeof Is Used with Brackets #8574

Closed
MrKWatkins opened this issue May 12, 2016 · 4 comments
Closed

Type Guards Not Recognised If typeof Is Used with Brackets #8574

MrKWatkins opened this issue May 12, 2016 · 4 comments
Labels
Bug A bug in TypeScript Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it

Comments

@MrKWatkins
Copy link

MrKWatkins commented May 12, 2016

TypeScript Version:

1.8.9, used via the Visual Studio extension 1.8.31.0

Code

function something(value: string | number)
{
    if (typeof (value) === "string")
    {
        alert(value.length);
    }
}

Expected behavior:
I would expect the compiler to treat the if statement as a type guard and recognise that value is a string afterwards.
Actual behavior:
I get the build error "Property 'length' does not exist on type 'string | number'". If I remove the brackets around value after the typeof, i.e.

function something(value: string | number)
{
    if (typeof value === "string")
    {
        alert(value.length);
    }
}

Then it compiles fine. I can therefore workaround this but it would be nice to not have to; I mainly write C# and as a result I automatically type brackets after typeof. :)

@mhegazy
Copy link
Contributor

mhegazy commented May 12, 2016

The compiler honors a certain forms of type guard expressions. there are other forms that are functionally equivalent, but are not supported, such as the one referenced in the OP. The recommendation would be to use the standard form instead.

@mhegazy mhegazy closed this as completed May 12, 2016
@mhegazy mhegazy added Bug A bug in TypeScript Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it labels May 12, 2016
@MrKWatkins
Copy link
Author

MrKWatkins commented May 16, 2016

Any chance you could reconsider please? It seems inconsistent with the rest of the language, plus it's quite difficult to work out what is happening because, as you say the forms are functionally equivalent. And I naturally use brackets after typeof due to many years of C#... Took me ages to figure out what was going on!

If you can't fix it could you maybe make it an error/warning to use brackets after typeof? Or at the very least put a big note in the handbook documentation telling people to use the standard form if they want type guards to work?

@mhegazy
Copy link
Contributor

mhegazy commented May 16, 2016

Any chance you could reconsider please?

it is not about this pattern, there are other ones:
typeof (v) === "string"
typeof v === ("string")
(typeof v) === "string"
typeof v === (foo(), "string")

If you can't fix it could you maybe make it an error/warning to use brackets after typeof?

it is valid JS/TS code. it just does not narrow the type. a tslint rule would be a simple addition that can catch these.

@MrKWatkins
Copy link
Author

No, fair enough, I can see how it would be complex to fix! If you could add a note to the handbook stating that type guards only work with the standard form that would be good though, might save others a bit of time working out what is going wrong.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it
Projects
None yet
Development

No branches or pull requests

2 participants