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.isLiteral() returns false for boolean literals #26075

Open
dsherret opened this issue Jul 31, 2018 · 7 comments
Open

type.isLiteral() returns false for boolean literals #26075

dsherret opened this issue Jul 31, 2018 · 7 comments
Labels
API Relates to the public API for TypeScript Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@dsherret
Copy link
Contributor

dsherret commented Jul 31, 2018

TypeScript Version: 3.1.0-dev.20180728 (new in 3.0.1)

Search Terms: isLiteral()

Code

  1. Create a boolean literal in the compiler api.
  2. Call isLiteral() on it.

Change seems to be done here:

e46d214#diff-233e1126c0abc811c4098757f9e4516eR428

Expected behavior: true for a boolean literal type (true or false)

Actual behavior: false

I have a few tests for assumptions about the compiler api in ts-simple-ast and this one started failing. Maybe it's not a bug, but I thought I would log it anyway to find out why this behaviour changed.

Is it because it doesn't have the Unit flag while string and number literals do? For what it's worth, in checker.ts it will internally return true for boolean literals in isLiteralType.

@ajafff
Copy link
Contributor

ajafff commented Jul 31, 2018

This is because a boolean literal type is actually not represented by a LiteralType but really is a IntrinsicType.
If you understand .isLiteral() as a type guard (is this an instance of LiteralType) instead of just a method that tells you if it originated in a literal, it kind of makes sense.

@DanielRosenwasser DanielRosenwasser added In Discussion Not yet reached consensus API Relates to the public API for TypeScript labels Jul 31, 2018
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.1 milestone Jul 31, 2018
@DanielRosenwasser DanielRosenwasser added the Suggestion An idea for TypeScript label Jul 31, 2018
@DanielRosenwasser
Copy link
Member

Let's chat whether we should fix this @weswigham.

@dsherret
Copy link
Contributor Author

dsherret commented Jul 31, 2018

@ajafff oh yeah, good point! These methods on Type are for type guarding after all.

Another point: the common LiteralType interface's value property was previously and currently has type string | number rather than string | number | boolean.

interface LiteralType extends Type {
    value: string | number;
    // ...
}

So isLiteral()'s new behaviour seems like a fix for the current design.

That said, as an API user it does seem a bit unexpected that boolean literal types aren't included with LiteralType, but I don't have a use case where it would matter.

@ajafff
Copy link
Contributor

ajafff commented Jul 31, 2018

That said, as an API user it does seem a bit unexpected that boolean literal types aren't included with LiteralType, but I don't have a use case where it would matter.

I need it quite often and it's annoying, see #22269

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Aug 23, 2018
@RyanCavanaugh
Copy link
Member

Consensus was that this function should return true for booleans as expected

@Kingwl
Copy link
Contributor

Kingwl commented Aug 24, 2018

is that mean we want boolean to be a literal?(booleanLiteral extends Literal and has a boolean 'value' prop)

@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
@marijnh
Copy link

marijnh commented Jul 8, 2019

I just ran into this too (needing to use undocumented internals to get at the value of a boolean literal type). So to second @Kingwl 's question—is the intent to add a BooleanLiteralType interface to the public API? Anything someone undertaking such a change should be careful about?

quisar pushed a commit to quisar/desynced-tools that referenced this issue Dec 20, 2023
Due to microsoft/TypeScript#26075 true and
false are not considered literal. This CL change the condition when
checking for the expression on the if statement to correctly handle
literable booleans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Relates to the public API for TypeScript Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

7 participants