1

I am trying out the ts-morph npm module to replace some code which I have already written but which overlaps ts-morph and is inferior. Nevertheless, I have some existing functions that take an ts.Node type arguments, mostly for exploration and discovery, which I need to use for reference while trying out ts-morph.

However, I can't see a way to access the underlying ts.Node instance from a ts-morph.SourceFile instance - there are no ts-morph functions with return type of ts.Node or ts.TypeChecker.

This doesn't work

      (sourceFile as unknown) as ts.SourceFile,
      (checker as unknown) as ts.TypeChecker,

because, for starters, (sourceFile as unknown) as ts.SourceFile doesn't have a kind member.

Is there a way access the underlying ts.Node instance from, e.g., ts-morph.SourceFile?

1 Answer 1

2

ts-morph provides access to all the underlying compiler API objects it wraps.

For any node, you can access the underlying compiler node using the compilerNode property:

sourceFile.compilerNode // ts.SourceFile

Note though that the underlying compiler node will become out of date whenever the source file is manipulated via ts-morph (ex. you add a class to the source file, remove a function, or other stuff like that).

https://github.com/dsherret/ts-morph/blob/af35677f3b498ed0f8e87e4b6c92a7246cfab210/packages/ts-morph/lib/ts-morph.d.ts#L3189

To get the TypeScript TypeChecker, use the compilerObject property on TypeChecker:

project.getTypeChecker().compilerObject // ts.TypeChecker
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent! I had been considering unwrap() but various obstacles were slowing progress on that front.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.