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

Without --strictNullChecks, x && "a"contributes a "" type. #20090

Closed
ghost opened this issue Nov 17, 2017 · 2 comments
Closed

Without --strictNullChecks, x && "a"contributes a "" type. #20090

ghost opened this issue Nov 17, 2017 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@ghost
Copy link

ghost commented Nov 17, 2017

TypeScript Version: 2.7.0-dev.20171116

Code

function f(b: boolean): false | "a" {
	return b && "a";
}

Expected behavior:

No error. (Just like with --strictNullChecks.)

Actual behavior:

src/a.ts(2,2): error TS2322: Type '"" | "a"' is not assignable to type 'boolean | "a"'.
  Type '""' is not assignable to type 'boolean | "a"'.
@ghost ghost added the Bug A bug in TypeScript label Nov 17, 2017
@ahejlsberg
Copy link
Member

This is working as intended for backwards compatibility reasons. Originally, the type of an && operation was simply the type of the right hand operand and a lot of old code depends on that. With literal types we change the falsy left hand side value into a falsy value in the same domain as the right hand side to best approximate the original behavior.

In general, you see a true reflection of the run-time behavior only in --strictNullChecks mode.

@ahejlsberg ahejlsberg added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Bug A bug in TypeScript labels Nov 17, 2017
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants