Ts-morph: "typeRoots" option is ignored

Created on 6 Sep 2018  路  12Comments  路  Source: dsherret/ts-morph

In tsconfig.json, the compilerOptions.types option seems to be ignored by ts-simple-ast.

ts-simple-ast API doesn't seem to get any type info from .d.ts files outside of the source root directory when using "types" option.

bug

Most helpful comment

@osyrisrblx I was looking into this and the typeRoots option is not relevant in that situation. For example, when the typeRoots option is removed it will still work. I think perhaps tsc is being a bit generous in what it includes, but I'm not sure why. Perhaps it includes all .d.ts files regardless of the rootDir.

typeRoots is supposed to work on packages like @types packages (see here) and so it doesn't search for files in the directory specified.

For example, to see the true behaviour of typeRoots:

  1. Move the typings folder up a directory so its a sibling of test-project. Then change typeRoots to "typeRoots": ["../typings"]. Run tsc and observe the compile error that it can't find Foo.
  2. Then within typings create a sub folder called foo and move foo.d.ts into it. Rename foo.d.ts to index.d.ts. Run tsc and see it compiles now.

If anything, perhaps this library should include all .d.ts files in the directory and sub directories of tsconfig.json, but I'm unsure if that's how tsc actually works. I'd need to look into it more.

All 12 comments

@osyrisrblx I'll look into this soon, but I feel like this should work. Could you show how ts-simple-ast is setup in your scenario?

@dsherret looks like if a .d.ts file is outside of the "rootDir", all of the defined types are picked up by ts-simple-ast as type "any".

Attempting to use Type.getSymbol(); returns undefined as well.

VSCode's built-in TypeScript seems to understand the types just fine which makes me think it's a ts-simple-ast bug.

@osyrisrblx how are you specifying the types compiler option in ts-simple-ast? I'll take a look soon.

@dsherret My tsconfig.json looks like this:

{
    "compilerOptions": {
        "strict": true,
        "outDir": "out",
        "rootDir": "src",
        "baseUrl": "src",
        "downlevelIteration": true,
        "noLib": true,
        "target": "ES5",
        "types": [
            "types",
        ]
    }
}

where "types" is a folder inside the same directory as "src" and "out".

I've also tried replacing "types" with "types/", "types/*", and "types/**/*"

@dsherret Yes, I supply a path to it when I create a Project object.

You can find the code here if it helps:
https://github.com/roblox-ts/roblox-ts/blob/master/src/class/Compiler.ts#L22

FWIW, I've been able to read properties like "baseUrl" from Project.getCompilerOptions().

@osyrisrblx ok thanks! Just making sure :)

You will want to use the "typeRoots" option and not "types". Read more here: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types

There was a bug with "typeRoots" not working though. That's fixed now in 14.4.3.

@dsherret Unfortunately, it looks like I 馃帀'd prematurely. This bug is still occurring for me, and I made a small repro to demonstrate it with 14.4.3:

https://github.com/osyrisrblx/415-repro

@osyrisrblx I was looking into this and the typeRoots option is not relevant in that situation. For example, when the typeRoots option is removed it will still work. I think perhaps tsc is being a bit generous in what it includes, but I'm not sure why. Perhaps it includes all .d.ts files regardless of the rootDir.

typeRoots is supposed to work on packages like @types packages (see here) and so it doesn't search for files in the directory specified.

For example, to see the true behaviour of typeRoots:

  1. Move the typings folder up a directory so its a sibling of test-project. Then change typeRoots to "typeRoots": ["../typings"]. Run tsc and observe the compile error that it can't find Foo.
  2. Then within typings create a sub folder called foo and move foo.d.ts into it. Rename foo.d.ts to index.d.ts. Run tsc and see it compiles now.

If anything, perhaps this library should include all .d.ts files in the directory and sub directories of tsconfig.json, but I'm unsure if that's how tsc actually works. I'd need to look into it more.

@dsherret Is there currently a way with ts-simple-ast to include .d.ts which sit outside of the rootDir?

@osyrisrblx doing this should work: project.addExistingSourceFiles("typings/**/*.d.ts");

Was this page helpful?
0 / 5 - 0 ratings