Constant types break isXType functions
See original GitHub issueI might be missing something obvious here… but getting types for constant variable declarations doesn’t work as expected, getting the apparent type doesn’t seem to resolve the problem.
Demo:
import Project, { TypeGuards } from 'ts-simple-ast'
const project = new Project()
const file = project.createSourceFile('file.ts', `
export const a = 'hi'
export const b = 1
`)
file.getExportedDeclarations()
.filter(TypeGuards.isVariableDeclaration)
.forEach(declaration => {
const type = declaration.getType()
const apparentType = type.getApparentType()
console.log(declaration.getName())
console.log(type.getText(), type.isNumberType(), type.isStringType())
console.log(apparentType.getText(), apparentType.isNumberType(), apparentType.isStringType())
console.log()
})
Output:
a
"hi" false false // => Expected false, true
String false false // => I'd like to get type string
b
1 false false // => Expected true, false
Number false false // => I'd like to get type number
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Classes: Type Constants - HHVM and Hack Documentation
Type constants provide a way to abstract a type name. However, type constants only make sense in the context of interfaces and inheritance...
Read more >Constant and Literal Data Types - Visual Basic - Microsoft Learn
A constant is a meaningful name that takes the place of a literal and retains this same value throughout the program, as opposed...
Read more >Why is constness not respected inside these julia functions?
For "strong typing" in functions, you'd use a type-declaration, not a const. local a::Float64 = 2.0 . That can't change types. – Chris...
Read more >const - JavaScript - MDN Web Docs - Mozilla
The const declaration creates block-scoped constants, much like variables declared using the let keyword. The value of a constant can't be ...
Read more >Constants - Manual - PHP
Prior to PHP 8.0.0, constants defined using the define() function may be case-insensitive. ... type the function name exactly (with the parentheses). <?php...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Found it! There’s a
getBaseTypeOfLiteralType()
onts.TypeChecker
.If you want, you can temporarily fall back to the compiler API…
…but I will open up an issue and wrap this soon.
Hi @Gerrit0, those are
TypeFlags.NumberLiteral
andTypeFlags.StringLiteral
types. TheisStringType()
is seeing if the type has a flag ofTypeFlags.String
, which in this case it doesn’t.I’ll add methods for
NumberLiteral
,StringLiteral
, andBooleanLiteral
because those are currently missing.