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

No boolean coercion compiler option #14114

Closed
brunnerh opened this issue Feb 16, 2017 · 2 comments
Closed

No boolean coercion compiler option #14114

brunnerh opened this issue Feb 16, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@brunnerh
Copy link

One of the things that always bugged me about JS and tends to cause bugs of various sorts (just had another one last week) is the boolean coercion that happens in if clauses or when using boolean operators like || or ?:.

Here is an example:

var predicate = () => false;
if (predicate)
	alert("true branch");
else
	alert("false branch");

This will compile without issue and always end up executing the true branch, because the developer merely referenced the predicate function instead of calling it.

Another example (this time simply bad code):

function test(optionA: string, optionB: number)
{
	// Set defaults if none provided
	optionA = optionA || "[None]";
	optionB = optionB || 1;

	alert(`${optionA}, ${optionB}`);
}
test("", 0)

Empty string and zero get converted to false and the defaults get assigned even though possibly valid values were provided.

Having a compiler option that prevents implicit conversions like these would easily prevent some bugs. This changes nothing about the emitted code and would be an optional type safety measure like noImplicitAny, so i do not think there would be many implications to worry about.

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Feb 16, 2017

#9041 or #7989 ?

@brunnerh
Copy link
Author

Did not see those when looking for duplicates, thanks.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 16, 2017
@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

2 participants