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.

getTypeParameters not working?

See original GitHub issue

We noticed on our doc tool work that generics extraction are broken in TS 4.2. I know TS 4.2 changes how aliases are handled a bit, so not surprised. But I’m not sure how to get type parameters now…

Maybe I’m missing something, probably, or maybe there is a bug in ts-morph? The main thing is that getTypeParameters doesn’t seem to work like it used to.

Describe the bug

Version: 4.2.2 TS + 10.0.2 ts-morph

To Reproduce

import * as tsm from 'ts-morph'
import { Project, ts } from 'ts-morph'

console.log(ts.version) // x.x.x <-- provide this

const project = new Project()
const sourceFile = project.createSourceFile(
  'foobar.ts',
  `
    export type A = Record<string,string>

  `,
)

const all = [...sourceFile.getExportedDeclarations()]

all.forEach(([name, decs]) => {
  console.log(name)

  const dec = decs[0]!
  console.log(dec.getText())

  const t = dec.getType()

  console.log('type alias dec -> type -> text', t.getText())
  console.log('type alias dec -> type -> sym -> dec -> text', t.getSymbol()?.getDeclarations()[0]?.getText())

  if (tsm.Node.isTypeAliasDeclaration(dec)) {
    const tp = dec.getTypeParameters()
    console.log(`type alias dec type parameters: ${tp.length}`)
  }

  const tr = dec.getChildrenOfKind(tsm.ts.SyntaxKind.TypeReference)[0]!

  console.log('type reference -> text', tr.getText())

  console.log('type reference -> type -> text', tr.getType().getText())

  console.log(
    'type reference -> type arguments -> text',
    tr.getTypeArguments().map((ta) => ta.getText()),
  )

  console.log('type reference -> type name -> text', tr.getTypeName().getText())

  console.log('type referene -> type -> sym -> dec -> text', tr.getType().getSymbol()?.getDeclarations()[0]?.getText())

  console.log(
    'type referene -> type -> sym -> dec -> kindName',
    tr.getType().getSymbol()?.getDeclarations()[0]?.getKindName(),
  )
})

Output:

4.2.2
A
export type A = Record<string,string>
type alias dec -> type -> text import("/Users/jasonkuhrt/projects/pakadev/paka/foobar").A
type alias dec -> type -> sym -> dec -> text {[P in K]:T;}
type alias dec type parameters: 0
type reference -> text Record<string,string>
type reference -> type -> text import("/Users/jasonkuhrt/projects/pakadev/paka/foobar").A
type reference -> type arguments -> text [ 'string', 'string' ]
type reference -> type name -> text Record
type referene -> type -> sym -> dec -> text {[P in K]:T;}
type referene -> type -> sym -> dec -> kindName MappedType

Expected behavior

Should be able to get type parameters of record via getTypeParameters.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dsherretcommented, Feb 26, 2021

I looked at this a bit and I’m not sure. I see the change between TS 4.1 and 4.2 on ts-ast-viewer.com https://ts-ast-viewer.com/#code/KYDwDg9gTgLgBDAnmYcCCcC8cBKwDG0AJgDwDOMUAlgHYDmANBdfQHxA

You may want to play around in the browser console with the checker object and node object to see if you’re about to figure it out.

1reaction
dsherretcommented, Feb 26, 2021

What’s the expected behaviour here? getTypeParameters() on the declaration will return the type alias node’s type parameter declarations (so it works exclusively in the AST)

Read more comments on GitHub >

github_iconTop Results From Across the Web

getTypeParameters returns a null TypeVariable array
getTypeParameters() returns the array of type parameters used in the method definition. It does not return the array of argument types.
Read more >
Method Class | getTypeParameters() Method in Java
getTypeParameters () method of Method class returns an array of TypeVariable objects declared by the generic declaration of this Method object, ...
Read more >
Archi-lab, get Type Parameters does not work, why? - Packages
Hello, for any reason it selects nothing… import clr clr.AddReference('ProtoGeometry') from Autodesk.DesignScript.
Read more >
Java Class class getTypeParameters() method with example
Parameter(s):. It does not accept any parameter. Return value: The return type of this method is TypeVariable[], it returns the following value ...
Read more >
TypeParamStash Fails To Detect Bindings of Inner Class #1732
TypeBindings, which attempts to discover the generic type parameters of a passed-in Class, does not work properly when dealing with an inner ...
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