question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Constant types break isXType functions

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
dsherretcommented, Mar 26, 2018

Found it! There’s a getBaseTypeOfLiteralType() on ts.TypeChecker.

If you want, you can temporarily fall back to the compiler API…

const baseType = project.getTypeChecker().compilerObject.getBaseTypeOfLiteralType(type.compilerType);

…but I will open up an issue and wrap this soon.

1reaction
dsherretcommented, Mar 26, 2018

Hi @Gerrit0, those are TypeFlags.NumberLiteral and TypeFlags.StringLiteral types. The isStringType() is seeing if the type has a flag of TypeFlags.String, which in this case it doesn’t.

I’ll add methods for NumberLiteral, StringLiteral, and BooleanLiteral because those are currently missing.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found