dsherret / dsherret/ts-morph

[question] Identifying the concrete source of a reference

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

Description

Hi there. I'm stumbling through trying to use ts-morph to generate some code from an interface. One thing I'm hoping to do is essentially annotate fields in the interface to guide some of the implementation. It seems like wrapping in a generic type reference would be a fairly ergonomic way to do this.

For example:
```ts
// type Annotated = T
import type { Annotated } from 'mylib';

interface Foo {
name: Annotated
}
```

One thing I'm struggling to figure out is how to determine if "Annotated" is the actual type I am providing, or just happens to have the same name as something the user defined. It seems like I might be able to find my way there through something like:

```ts
interfaceDeclaration.getProperties().map(prop => {
const node = prop.getTypeNode();
if (node.getKind() === SyntaxKind.TypeReference) {
const sourceFile = n.getSourceFile();
// there seems to maybe be the information I need to make a distinction here?
}
});
```

However, I'm not entirely sure if this is a reasonable approach, and I can't seem to determine the methods that tell me concretely "in this context, `Annotated` is a reference to the type I've provided".

Is there a better way to approach this? If this is the best approach, what methods am I looking for to decide affirmatively that I "care" about this type?

(also, is there an IRC channel or Discord where I could go for help as I work through this stuff?)

Contributor guide

Open the contributing guide

Research direction

Start with the interfaceDeclaration.getProperties(), prop.getTypeNode(), and TypeReference calls shown in the report, then trace the relevant ts-morph type and source-file APIs. Done means identifying a reliable way to distinguish the imported Annotated type from a same-named user-defined type and documenting the appropriate approach.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.