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.

make getTargetType return undefined if would return self

See original GitHub issue

Describe the bug

Version: 9.1.0

To Reproduce

import dedent from 'dedent'
import * as tsm from 'ts-morph'
import { dumpType } from './utils'

const tsMorphProject = new tsm.Project({
  skipAddingFilesFromTsConfig: true,
  useInMemoryFileSystem: true,
})

tsMorphProject.createSourceFile(
  '/repro-example.ts',
  dedent`
    export interface Foo<T> {}
  `
)

tsMorphProject.addSourceFileAtPath('/repro-example.ts')

tsMorphProject.getSourceFiles().map((sf) => {
  sf.getExportedDeclarations().forEach((ex) => {
    const t = ex[0]!.getType()!
    dumpType(t)
  })
})

My log:

t.getText()                                          = import("/repro-example").Foo<T>
t.getSymbol()?.getName()                             = Foo
t.compilerType.getFlags()                            = 524288
(t.compilerType as tsm.ts.ObjectType)?.objectFlags   = 6
t.getAliasSymbol()?.getName()                        = undefined
t.getApparentType().getText()                        = import("/repro-example").Foo<T>
t.getSymbol()?.getDeclarations()?.[0]?.getKindName() = InterfaceDeclaration
t.getSymbol()?.getDeclarations()?.[0]?.getText()     = export interface Foo<T> {}

t.isAnonymous()                                      = false
t.isAny()                                            = false
t.isInterface()                                      = true
t.isObject()                                         = true
t.isArray()                                          = false
t.isBoolean()                                        = false
t.isBooleanLiteral()                                 = false
t.isClass()                                          = false
t.isClassOrInterface()                               = true
t.isEnum()                                           = false

t.getAliasTypeArguments().length}                    = 0
Boolean(t.getTargetType())                           = true
t.getTargetType() === t                              = true

t.getText(undefined, tsm.ts.TypeFormatFlags.InTypeAlias)                           = Foo<T>
t.getText(undefined, tsm.ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope)    = Foo<T>
t.getText(undefined, tsm.ts.TypeFormatFlags.UseTypeOfFunction)                     = Foo<T>
t.getText(undefined, tsm.ts.TypeFormatFlags.UseFullyQualifiedType)                 = import("/repro-example").Foo<T>
t.getText(undefined, tsm.ts.TypeFormatFlags.NoTruncation)                          = Foo<T>
t.getText(undefined, tsm.ts.TypeFormatFlags.UseStructuralFallback)                 = Foo<T>

Target Type?
------------
N/A

Notice that I mark Foo has having no target type here (N/A) because the target type refers to self via reference equality.

Expected behavior

This looks like a bug to me. It seems that calling getTargetType in this case should return undefined (or maybe slightly better null). It seems to me that getTargetType should recursively return values until the terminal non-instantiated type-with-generics is reached.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dsherretcommented, Dec 15, 2020

@jasonkuhrt sure! Yeah, on the JS doc just saying it will return itself if it’s already the target type.

0reactions
jasonkuhrtcommented, Dec 15, 2020

Ok understood, maybe we can mention this on the docs or something. Would you be open for a PR for that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why in JavaScript "THIS" Returning value instead of undefined.?
According to me p.prototype.getName() should be return undefined, because "THIS" is targeting prototype object instead of p and prototype don't ...
Read more >
Why is Javascript Function Return Undefined - Dev Genius
In the next row, I call the function, and then we see the console.log output 'code execution' and finally the returned value output...
Read more >
undefined - JavaScript - MDN Web Docs - Mozilla
A function returns undefined if a value was not returned . Note: While you can use undefined as an identifier (variable name) in...
Read more >
https://raw.githubusercontent.com/Microsoft/TypeSc...
If target is not transient, mergeSymbol will produce a transient clone, mutate that and return it. */ function mergeSymbol(target: Symbol, source: Symbol): ...
Read more >
Overlapping segments of 'laravel.com/js/app.js?id ... - Bundle Scanner
getOwnPropertyNames(Symbol).map((e=>Symbol[e])).filter(rt)),Tt=Lt(),kt=Lt(!1,!0),Nt=Lt(!0),It=Lt(!0,!0),Pt={};function Lt(e=!1,t=!1){return function(n,r ...
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