TypeScript: Auto imports from referenced project not working until already imported once in project
TS Template added by @mjbvz
TypeScript Version: 4.0.0-dev.20200727
Search Terms
- angular
- project references
- auto imports
- VSCode Version: 1.47.2
- OS Version: Mac OS Catalina 10.15.5
Steps to Reproduce:
I have an Angular project, and just 1 of the files don’t have a proper suggestion support in it. Same happens when I create a file via plus sign from the side bar. VSCode doesn’t suggest “obvious(necessary)” things, quite the contrary happens: it suggests everything else what’s available, but not what I need and not what really exists in the project.
Also, when I try to import environment object with ‘src/environments/environment’, it doesn’t seem to know about src folder(project_name/src). Although it already works this way for all the other files in the project.
Cache cleaning && insiders build didn’t help out, no errors in debug console and I couldn’t find any useful tsserver logs. Although I can send if necessary. Having 3.9.7 TS
Does this issue occur when all extensions are disabled?: Yes
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 22
- Comments: 27 (10 by maintainers)
Any update on this? How can Microsoft’s IDE (written in TypeScript!) not properly support Microsoft’s TypeScript 🤯
Project reference imports like using
@namespace only works when there is already an import using that path. Otherwise it defaults to relative paths. Very annoying to have to manually type it out initially everytimeConfirm. Autocomplete not working via references in tsconfig.json
demo: https://github.com/zerkalica/ts-references-autoimport-bug/tree/autoimport-no-ref
Currently autocomplete works only with includes (cpu eating on big projects) and via “types” in package.json.
Autocomplete doesn’t works with direct imports (without types in package.json)
Is there any workaround to this in the meantime? I read somewhere about setting
baseUrlto the parent directory of both projects but it didn’t helpI found autoimports working quite reliably if top level tsconfig includes all files. It will be unused by any particular file or project having own local configuration, but magically enables autocomplete.
Also almost the opposite advice - do not reference workspace packages from a top-level config. Even for small projects it might take too much time to start language server, so you’ll be left with no support as-you-type.
Looks like
src/environments/environment.tsandapp.component.tsare part of separate tsconfig projects, although both of these project are both referenced bytsconfig.json.Moving upstream to get the TypeScript team’s feedback on this behavior
So that the trick you should have your
tsconfig-baseas yourtsconfigand put references aside (tsconfig.references.json) using them only inside particular packages.I am not sure references are expected to work this way, but it works.
@andrewbranch
Sure, here’s a repo that has the issue.
This is based on a simplification of our own yarn monorepo. We’ve got a
commonproject that contains stuff that’s shared between frontend and backend (thebackendproject is omitted), and awebproject that references it via a@commonalias. Each project has its owntsconfig.json. I believe this is pretty similar to the example repo posted above. To see the problem, hop intoweb/src/Helper.tsand try to use a “Quick Fix” to importsquarefrom thecommonrepo.This example repo doesn’t include any of this extra stuff because it suffices to show the issue, but for context, in our real repo
webis a CRA-based Webpack app. We run it viayarn tsc --build --watch & react-app-rewired start(we runtscseparate becauseCRA’s setup doesn’t understand building projects). We want to import the compiled code incommon/distvia an alias (e.g.import { square } from '@common/MyModule').Our biggest issue is the one mentioned in the comment above - VSCode auto-import suggests relative paths to the original
.tsfile no matter what. There’s a few issues here:.jsfile, not the original.tsfileWhat I mean by that last point is, if I try it with the “Import Module Specifier” preference set to “shortest”, it works (although with a relative path, which is still undesirable):
But if I set “Import Module Specifier” to “non-relative”, it curiously gives me:
Which is simply broken - an immediate compilation error.
It’s totally possible - and would be great if! - this is just a configuration issue on our part and there’s a different/better way to do it.
Thanks for your help!
any update here? my team would really like this issue fixed.