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

Check typeof operator result #12697

Closed
mohsen1 opened this issue Dec 6, 2016 · 8 comments
Closed

Check typeof operator result #12697

mohsen1 opened this issue Dec 6, 2016 · 8 comments
Labels
Duplicate An existing issue was already created

Comments

@mohsen1
Copy link
Contributor

mohsen1 commented Dec 6, 2016

Throw a compiler error if typeof operator is compared to strings other than typeof operator results

TypeScript Version: 2.1.1

Code

typeof a === 'String' // error
typeof b === 'null' // error
@mhegazy
Copy link
Contributor

mhegazy commented Dec 6, 2016

I am sure we talked about this before but can not find the issue right now. The issues is this section:

Implementation-defined. Must not be "undefined", "boolean", "function", "number", "symbol", or "string".

It is allowed for engines to return some value outside this range. if you are using one of these engines, your code will be flagged as an error and you will need to cast to any.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Dec 6, 2016
@mohsen1
Copy link
Contributor Author

mohsen1 commented Dec 6, 2016

Can it be a compiler flag then? Similar to noUnusedLocals

@mhegazy
Copy link
Contributor

mhegazy commented Dec 7, 2016

:D well, everything can be done under a flag. but we are getting strong feedback from customers that the number of flags is getting to be overwhelming, and they are having trouble what they need to set. So we would like to stay away from flags when possible.

@mohsen1
Copy link
Contributor Author

mohsen1 commented Dec 7, 2016

That's fair.

Is there any engine in the wild that actually produce something outside of spec for typeof results?

@mhegazy
Copy link
Contributor

mhegazy commented Dec 7, 2016

Not that I know of. That is why I want to bring this back to discussion. I would expect this to catch more errors than block valid scenarios.

@yortus
Copy link
Contributor

yortus commented Dec 7, 2016

@mhegazy the previous discussion about this is in #9506 and #10594.

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Dec 7, 2016

@mhegazy With respect to the increasing number of flags, TypeScript is still simpler to configure than a lot of other tools, but regardless it certainly could be simpler than it is. Perhaps a notion of "presets", should be introduced. Perhaps this could also leverage the new configuration inheritance support (I'm not sure, might obscure things).

I am thinking something like.

presets = Node | NodeLoose | AMD | AMDLoose | ...

where the non-loose variants would be strict defaults that contained

{
  "compilerOptions":  {
    "strictNullChecks": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "noUnusedLocals": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "allowUnusedLabels": false
  }
}

@mhegazy
Copy link
Contributor

mhegazy commented Dec 7, 2016

thanks @yortus for the link. looks like this was already discussed in details in #10594, the recommendation is to use a tslint rule.

@mhegazy mhegazy added Duplicate An existing issue was already created and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Dec 7, 2016
@mhegazy mhegazy closed this as completed Dec 7, 2016
@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants