Skip to content

Typeof comparison expression should check right hand side value #9506

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

Closed
tinganho opened this issue Jul 4, 2016 · 5 comments · Fixed by #13791
Closed

Typeof comparison expression should check right hand side value #9506

tinganho opened this issue Jul 4, 2016 · 5 comments · Fixed by #13791
Labels
Committed The team has roadmapped this issue Suggestion An idea for TypeScript

Comments

@tinganho
Copy link
Contributor

tinganho commented Jul 4, 2016

As I understood typeof operator can only return undefined, object, boolean, number, string, symbol, function.

If the RHS is a string literal, I think it should check against the above values and throw an error on:

const str = 'str';
if (typeof str === 'd') {// error right hand side can only be `undefined`, `object`, ...
}
@mhegazy mhegazy added the Suggestion An idea for TypeScript label Jul 6, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Jul 6, 2016

There are two parts here, first type of the return type of typeof operators to be "undefined" | "object" | "boolean" | "number" | "string" | "symbol" | "function" instead of string which sounds safe.

and two is to make a comparison between two a string literal type and a string literal an error, which would be a breaking change and prevent defensive coding patterns.

@mhegazy mhegazy added the In Discussion Not yet reached consensus label Jul 6, 2016
@DanielRosenwasser
Copy link
Member

There are two parts here, first type of the return type of typeof operators to be "undefined" | "object" | "boolean" | "number" | "string" | "symbol" | "function" instead of string which sounds safe.

For the record, in the spec's section on The typeof Operator:

Type of val Result
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
Symbol "symbol"
Object (ordinary and does not implement [[Call]]) "object"
Object (standard exotic and does not implement [[Call]]) "object"
Object (implements [[Call]]) "function"
Object (non-standard exotic and does not implement [[Call]]) Implementation-defined. Must not be "undefined", "boolean", "function", "number", "symbol", or "string".

NOTE
Implementations are discouraged from defining new typeof result values for non-standard exotic objects. If possible "object" should be used for such objects.

@RyanCavanaugh
Copy link
Member

Good comments from Brian in #10594

@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Sep 19, 2016
@RyanCavanaugh
Copy link
Member

Until this is normatively specified we don't want to encode this into the type system. A TSLint rule would be very easy to write.

@RyanCavanaugh RyanCavanaugh added the Revisit An issue worth coming back to label Jan 31, 2017
@RyanCavanaugh RyanCavanaugh added Committed The team has roadmapped this issue Suggestion An idea for TypeScript and removed Revisit An issue worth coming back to Working as Intended The behavior described is the intended behavior; this is not a bug labels Aug 12, 2017
@RyanCavanaugh
Copy link
Member

We did do this! 🎉

@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
Committed The team has roadmapped this issue Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants