dsherret / dsherret/ts-morph

Question? How to extract type parameters from this case

Open
#910 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

In this case the type of `Foo` becomes interned to `Qux<1, 2> & 2`. This type is seen as an intersection. Its `getText()` returns `Bar<1,2>`. Its symbol name is `Bar`. Its `getTargetType` returns `undefined`.

```ts
type Qux = {}
type Bar = Qux & B
export type Foo = Bar<1, 2>
```

My question is this, then... How can we write logic to extract type parameters?

The logic I had before was:

1. If `getTargetType` returns another type then try extracting type arguments from self via `getTypeArguments`
1. otherwise for aliases, classes, interfaces try extracting type parameters via `getTypeArguments`

The idea that I could identify the generic type by checking `t.getTargetType() === t` turns out to not always hold. For example here `Qux` object type returns undefined for `t.getTargetType()`.

The problem then is that I am in a situation where I can't tell the difference between `Foo<1, 2>` (should extract type arguments) and `Qux` (should extract type parameters).

... I am probably doing something wrong, but I can't tell what it is. I'm posting here on the off chance that ts-morph has a missing feature or bug.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.