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.

How to get non-qualified type text?

See original GitHub issue

I’m not sure what changed in my setup, perhaps a TypeScript upgrade, or a ts-simple-ast upgrade, but today I noticed that my script no longer works as intended, and when I get the text of the type of a class property I get an import path in front of it.

I wasn’t able to find how to just get the non-qualified type. I must be missing something basic. Any help is appreciated.

This is my code:

const properties = _(cls.getInstanceProperties())
  .filter(prop => {
    return prop.getKind() === ts.SyntaxKind.PropertyDeclaration;
  })
  .forEach(prop => {
    console.log(prop.getType().getText());

The above outputs qualified types for some of the properties:

import("/path/to/node_modules/@angular/core/src/event_emitter").EventEmitter<Event>

But previously I was just getting:

EventEmitter<Event>

How can I get the latter again?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
dsherretcommented, Oct 3, 2018

Yeah, this was a change in TypeScript in 2.9. The key here seems to be to pass in the enclosing declaration to getText() which will cause it to not return the import type text:

import { Project, TypeFormatFlags } from "ts-simple-ast";

const project = new Project({ useVirtualFileSystem: true });
const sourceFile = project.createSourceFile("Class.ts", "export class Class { prop: string; }");
const mainFile = project.createSourceFile("main.ts", "import { Class } from './Class'; let c: Class;");

const varDec = mainFile.getVariableDeclarationOrThrow("c");
console.log(varDec.getType().getText()); // outputs: import("/Class").Class
console.log(varDec.getType().getText(varDec)); // outputs: Class

There might be a way to make this work using TypeFormatFlags alone (second param of getText)… I’m not sure.

0reactions
dsherretcommented, Oct 27, 2018

That’s very interesting! It seems what matters here is what type is requested first to the type checker and that’s the main problem here. I bet that’s probably by design, but opened #480 to investigate further.

Closing this for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nonqualified Stock Options | H&R Block
Learn more about reporting non-qualified stock options and get tax answers at H&R Block.
Read more >
Nonqualified Deferred Compensation Plans (NQDCs)
NQDC plans have the potential for tax-deferred growth, but they also come with substantial risks, including the risk of complete loss of the ......
Read more >
What Are Non-Qualified Stock Options (NSOs)? - Carta
Non-qualified stock options (NSOs) are a type of equity compensation that does not qualify for favorable tax treatment. Learn about NSOs and ...
Read more >
Is My Pension or Annuity Payment Taxable? - IRS
Determine if your pension or annuity payment from an employer-sponsored retirement plan or nonqualified annuity is taxable.
Read more >
Qualified vs Non-Qualified Annuities | Taxation and Distribution
Qualified annuities receive treatment similar to tax-favored retirement plans, while non-qualified annuities have fewer restrictions.
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