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

TypeChecker returns incorrect flags & types on boolean type #14668

Closed
fis-cz opened this issue Mar 15, 2017 · 3 comments
Closed

TypeChecker returns incorrect flags & types on boolean type #14668

fis-cz opened this issue Mar 15, 2017 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@fis-cz
Copy link

fis-cz commented Mar 15, 2017

TypeScript Version: 2.2.1

While using the API & TypeChecker I have noticed strange behavior for boolean type in AST tree (see bellow). Is it correct? If so, is there a simple way how to reconstruct the original TypeScript code form AST with the boolean type instead of union type?

Code

Code used in test file:

function(): boolean { return false; }
function(): true | false { return false; }
function(): string | boolean { return "" }

Code using API:

let program: ts.Program = ts.createProgram(...);
let checker: ts.TypeChecker = program.getTypeChecker();
...
ts.forEachChild(node, (n: ts.Node) => {
   ...
   let t: ts.Type = checker.getTypeAtLocation(n.type);
   ...
}

Expected behavior:
for boolean:

t: ts.Type === {
   ...
   flags: TypeFlags.Boolean
   types: undefined
   ...
}

for: true | false

t: ts.Type === {
   ...
   flags: TypeFlags.Boolean | TypeFlags.Union
   types: [ { ... false ...}, { ... true ... } ]
   ...
}

for: string | boolean

t: ts.Type === {
   ...
   flags: TypeFlags.Union
   types: [ { ... string ...}, { ... boolean ... } ]
   ...
}

Actual behavior:
for: boolean (Incorrect)

t: ts.Type = {
   ...
   flags TypeFlags.Boolean | TypeFlags.Union
   types: [ { ... false ... }, { ... true ... } ]
   ...
}

for: true | false (Correct)

t: ts.Type = {
   ...
   flags: TypeFlags.Boolean | TypeFlags.Union
   types: [ { ... false ... }, { ... true ... } ]
   ...
}

for: string | boolean (Incorrect)

t: ts.Type = {
   ...
   flags: TypeFlags.Union
   types: [ { ... string ...}, { ... true ... }, { ... false ... } ]
   ...
}
@fis-cz fis-cz changed the title TypeChecker returns incorrect flags on boolean type TypeChecker returns incorrect flags & types on boolean type Mar 15, 2017
@ahejlsberg
Copy link
Member

The actual behavior you're seeing is intended. There is no difference between boolean, false | true, and true | false, they're just different ways of referencing the exact same type. In fact, boolean is effectively just a predefined type alias for false | true.

@ahejlsberg ahejlsberg added the Question An issue which isn't directly actionable in code label Mar 15, 2017
@ahejlsberg
Copy link
Member

And, to answer your second question, there is no way to get back to the original AST representation from a type object.

@fis-cz
Copy link
Author

fis-cz commented Mar 15, 2017

Thanks for answer. A was afraid of it :) Actually, there is one way, but not ideal - it is .getText() of symbol which returns the text from the source file (parsed token).

@fis-cz fis-cz closed this as completed Mar 15, 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
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants