-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
typeRoots is not finding custom declaration file #22217
Comments
For modules, they have thier own scope, all you need is path mappig.. somethign like: {
"compilerOptions": {
"target": "es5",
"baseUrl": "./",
"paths": {
"*" : ["src/@t`ypes/*"]
}
}
}` |
Thanks for clearing that up @mhegazy. I think it would be helpful if this was explained more in the TypeScript docs, possibly under the declaration files section. |
TypeRoots is really a support we have added for back compat with typings and to allow migration, so we tried to keep it out of the docs as much as possible to avoid confusion. Not sure if this fits in the declaration file section. we do have a note about it in https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping I can add a note in the declaration file section about module resolution section as a whole. give that section a read and let us know if you still find docs in it lacking |
Oh I see. Yeah, that is the page that I needed to begin with. I think it would be nice to have at least a brief mention of path mapping and maybe a link to that doc somewhere under https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
I still think it would be helpful to have the docs updated as stated in my previous comment @typescript-bot |
@mhegazy Having a supported feature with little-to-no documentation is what drives confusion. Please properly document features like this but be liberal with your warning language. For example:
This would be vastly preferable to having to scan GitHub Issues, StackExchange, etc. for information. |
Thx a fucking ton @mhegazy ! 🎉 That path entry for |
If you don't mind slow down your application starting process little, you can just add |
I'm now getting:
and this option is being deleted. |
@KrzysztofMadejski that's done by CRA, not TS itself. |
This thread says that typeroots is really for backwards compatibility. I am trying to use it to author types for definitively typed. How else would I properly test these types without typeroots? I have generated types using |
@Roaders did you find the solution to use Edit: Solved with #22217 (comment) |
One thing I don't understand here is that: tsconfig.base.json:
And a directory structure:
It won't get picked up at all in my nrwl/nx repository. I can try a million things but if I move that Marking typeRoots as deprecated and saying it works in a weird/quirky way would have at least stopped me trying to use it. The only way I've managed to add local types each time I've needed to, is by spending hours fighting with my tsconfig.base.json and stumbling upon the mhegazy comment above |
I kept getting bitten by this and forgetting about having to configure the |
I'm being bit by this as well. I couldn't get custom types for a 3rd party project picked up no matter what I tried. It was working for a while by copying the folder into
This seems to work but it was extremely unintuitive and under-documented. It didn't occur to me to try |
I have a library written in javascript, which exposes type declarations to external consumers. These type declarations are generated automatically using the typescript compiler in a I initially thought that as @mhegazy suggested, I tried using The below does not work
but the below code works:
This also works:
Note that both of the working versions require some change in my webpack module resolution as well to work correctly, but at least typescript seems to be resolving them correctly. Am i missing something here, or is it generally not possible to make |
In my case, the issue was that I've been using so I just remove the |
@gaurav5430's comment got me on the right track! My original folder layout:
My {
"compilerOptions": {
"baseUrl": "./",
"declaration": true,
"esModuleInterop": true,
"module": "commonjs",
"outDir": "dist",
"paths": {
"*": [
"@types/*"
]
},
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "es5",
"typeRoots": [
"node_modules/@types",
"@types"
],
},
"include": [
"src/**/*"
]
} I was still getting errors and I could not understand why. Turns out, (Contents of that file being FINALLY Also, this issue is super annoying. |
If a project lacks types for one or more dependencies, it is customary to fill in those types via ambient modules in the form of `.d.ts` files. Unfortunately `.d.ts` files aren't 100% supported in this template. * Because `.d.ts` don't export anything, ESLint will complain. * A `.d.ts` file can contain multiple ambient modules. However, TypeScript may not automatically pick up the types in this file (unless that file is covered by the `include` setting, but even then it's not guaranteed it'll be seen before the file that needs to use the types contained in the `d.ts` file is seen). To address these issues: * We instruct ESLint to treat `.d.ts` files as scripts rather than modules. * We establish a `types/` directory where our ambient modules will be stored, and then update `tsconfig.json` to automatically consult this directory when resolving types for imported modules. (Note that we do *not* use the `typeRoots` option for this [as the `tsconfig.json` documentation might indicate][1]; this is [not intended to be used for this purpose][2]. Rather, we use `paths`.) * We update `tsconfig.json` to widen its purview for development so that test files have access to all of the types that non-test files do. * We add a special `tsconfig.build.json` file that is only used by `yarn build` which continues to ensure that only the files we want to publish (i.e. files in `src/`) are emitted to `dist/`. [1]: https://www.typescriptlang.org/tsconfig#typeRoots [2]: microsoft/TypeScript#22217
If a project lacks types for one or more dependencies, it is customary to fill in those types via [ambient modules][1] in the form of `.d.ts` (type definition) files. Unfortunately this template cannot be used to properly provide ambient modules. * First, because type definition files don't export anything, and ESLint is configured to read `*.ts` files as modules by default, ESLint will complain that a `.d.ts` file could be parsed as a script instead. That's an easy problem to solve. * Second, TypeScript's treatment of type definition files is not always intuitive, and we haven't fully understood how to use them. We usually work with type definition files in the context of packages, so the mechanics of how they work are kept out of sight. In that case, [TypeScript will use the `types` field in `package.json`][2] to know how to find the type definition files for a package. But what about backfilling types for existing packages (i.e. declaration of ambient modules)? We've been assuming that as long as a `.d.ts` file is matched by the `include` setting in `tsconfig.json`, that TypeScript will be able to see and use all of the types in that file. For instance, if you're importing a module `foo` in one `.ts` file, and you've backfilled the types for `foo` in another `.d.ts` file in the same directory, and both files are covered by `include`, then TypeScript should be able to bind types to the objects you're importing from `foo`, right? Not always. In fact, if you have a test file that is *not* covered by `include` that also imports the module `foo`, then TypeScript seems to ignore your type definition file and does not associate any types with `foo`. * Lastly, this problem illustrates that writing TypeScript and building TypeScript are two separate processes managed by two different tools (`tsserver` vs. `tsc`, respectively) which follow different rules and have different behavior. We need to acknowledge this in our configuration. So, here's what this commit does to address these issues: * We instruct ESLint to treat `*.d.ts` files as scripts rather than modules. * We establish a `types/` directory where our ambient modules will be stored, and then update `tsconfig.json` to automatically consult this directory when resolving types for imported modules. (Note that we do *not* use the `typeRoots` option for this [as the `tsconfig.json` documentation might indicate][3]; this is [not intended to be used for this purpose][4]. Rather, we use [`paths`][5].) * We update `tsconfig.json` to widen its purview for development so that test files have access to all of the types that non-test files do. * We add a special `tsconfig.build.json` file that is only used by `yarn build` which continues to ensure that only the files we want to publish (i.e. files in `src/`) are emitted to `dist/`. [1]: https://www.typescriptlang.org/docs/handbook/modules.html#ambient-modules [2]: https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html#editing-the-packagejson [3]: https://www.typescriptlang.org/tsconfig#typeRoots [4]: microsoft/TypeScript#22217 [5]: https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping
Has anyone succeeded in making this work with a target set to ES6? |
for anyone finding this issue i was able to resolve this in my code base by adding custom tsconfig.base.json {
"compileOnSave": false,
"compilerOptions": {
"rootDir": ".",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"module": "esnext",
"lib": ["es2017", "dom"],
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"@prisma-client": ["packages/libs/prisma-client/src/index.ts"],
"@ui/meeting": ["packages/libs/ui/meeting/src/index.ts"],
"@ui/shared": ["packages/libs/ui/shared/src/index.ts"],
"@ui/util": ["packages/libs/ui/util/src/index.ts"]
},
"typeRoots": ["types", "node_modules/@types"],
"types": ["@emotion-types", "@window-types"]
},
"exclude": ["node_modules", "tmp"],
"files": [],
"include": []
} SO post - second answer |
@ALFmachine big ups for the |
how to resolve? |
TypeScript Version:
Version 2.6.2
Search Terms:
declaration file for module exporting function, external module, npm package, third-party module
Code
src/testFile.ts
:src/@types/graphql-list-fields/index.d.ts
:Expected behavior:
Compiling without errors.
Actual behavior:
Error when running
tsc
:graphql-list-fields
is an NPM package that I'm trying to use, which currently has no published type declarations.After reading through the Typescript Handbook's section on declaration files, I found that I am trying to write a declaration file for a modular library and should use the module-function.d.ts declaration file template. This is how I came up with the code in
index.d.ts
above, but the compiler still complains about not having the declaration file for the module 'graphql-list-fields'. After trial and error, we found that addingdeclare module 'graphql-list-fields'
around everything worked without compiler errors.We tried setting
"typeRoots": ["./node_modules/@types", "./src/@types"]
intsconfig.json
, as mentioned in the tsconfig docs but that still did not work withoutdeclare module 'graphql-list-fields'
. This seems to be an issue because tsc is not finding the type declaration in the directories specified in typeRoots.Related Issues:
3019, 8335
The text was updated successfully, but these errors were encountered: