codeql icon indicating copy to clipboard operation
codeql copied to clipboard

Question: How to make getType() work in TypeScript

Open jpoly1219 opened this issue 1 year ago • 5 comments

Hello, I was hoping to get some help about using getType() in Typescript files. The docs say the following:

Static type information
Static type information and global name binding is available for projects with “full” TypeScript extraction enabled. This option is enabled by default when you create databases with the CodeQL CLI.

I don't seem to be able to get any calls to .getType() to work. Am I missing a setup process to enable the "full" TypeScript extraction? How should I set that up?

For more context, the following query does not return anything:

from Type t
select t

I believe this is the reason why .getType() isn't returning anything, since there are no Type classes to be found. What is the Type class and what makes a TypeScript codebase have one?

Thank you!

jpoly1219 avatar Jun 26 '24 22:06 jpoly1219

Hi @jpoly1219

Thanks for your question. Do you maybe have some example project from which you're missing type information? I expect the type information to be there if your database was created from a TypeScript codebase.

jketema avatar Jun 27 '24 08:06 jketema

Hi @jketema! Unfortunately I can't share code due to... reasons :) I will say that I have a tsconfig.json in the root directory, but I created a database in a subdirectory that gets ignored when compiling with tsc (I added the subdirectory to my excluded list). Does getType() rely on a d.ts file?

Otherwie, is there a setting that I need to change, or some dependencies that I need to install? If I know what allows for the getType() to work, then I could try fixing my codebase. If there are things I can do for you, please let me know!

I used the following commands to make my database:

codeql database create /path/to/subdirectory/mydb --source-root=/path/to/subdirectory --languge=javascript-typescript --overwrite

Thank you for your patience!

jpoly1219 avatar Jun 30 '24 19:06 jpoly1219

In that case please create a small example that demonstrates the problem.

jketema avatar Jun 30 '24 19:06 jketema

Ok, I made a small example. Check it out here: https://github.com/jpoly1219/codeql-gettype-example

After cloning and running npm install in the root, you should be able to run the query inside the codeql-custom-queries-javascript. The database is created using the command codeql database create subdir/exampledb --source-root=subdir --languge=javascript-typescript. I am using VSCode's CodeQL extension to run my query.

jpoly1219 avatar Jun 30 '24 19:06 jpoly1219

Thanks for the small example. So there needs to be a tsconfig.json file in your /path/to/subdirectory for getType() to work.

jketema avatar Jul 01 '24 08:07 jketema