Open niba opened 5 years ago
Any news on this? I have a common
directory for files that are used by react
and react-native
in other 2 directories (both use typescript), and I'm able to import them automatically only in the common
directory, if I try to import something present in the common
directory in one of the 2 other directories, it will only work if it was already imported, otherwise it won't auto-import, not even show a suggestion.
Also, if I try to find the references of something in the common
directory, it will only find the references in that directory, even if there are places calling it in the react
and react-native
directories (I can only see those dependencies if I try to find them in some place it's already being used in one of those 2 directories).
For example, in my tsconfig.json
in the react
directory, I have:
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/lang/*": [
"./lang/*"
],
"@/lib/*": [
"../common/lib/*"
]
},
...
Typescript correctly sees the dependencies at @/lib/*
, but doesn't autoimport them if the dependency is not being referenced anywhere.
Any suggestions about how can I proceed?
Update
I was able to make it work by placing a link in react/lib
pointing to common/lib
and changing tsconfig.json
to:
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/lang/*": [
"./lang/*"
],
"@/lib/*": [
"./lib/*"
]
},
...
This solved both the problems from auto-imports (this issue), as well as when trying to find references from dependencies in the common
directory being used in the react
directory (before, only the references in the common
directory were shown).
TypeScript Version: 3.3.3 or 3.4.0-dev.20190311
Search Terms: quick fix, import, project references, path aliases
Code: I use project-references-demo
Problem 1 Import quick fix doesn't appear. Modification to the original code Removed makeRandomName import from
dog.ts
file. Added lastElementOf dummy function to core/utilites.ts. Description In the projectanimals
I would like to use vscode and quick fix to add import formakeRandomName
from projectcore
. Vscode doesn't suggest anything.To fix the problem I need to import once (it can be anywhere in my project) from the file that contains
makeRandomName
. For example if I addimport { lastElementOf } from "../core/utilities";
toanimal.ts
it will solve my problem. After that indog.ts
I get a suggestion toImport makeRandomName from module "../lib/core/utilities"
.Expected behavior: I expect to immediately get import suggestions.
Problem 2 Import quick fix suggests wrong paths in a project with path aliases Modification to the original code In zoo project I added the following entries to the tsconfig
Description When I want to reference something from the project itself I get suggestions from quick fix to use
@app/{projectPath}
and it's correct.The problem is with imports to the
animals
project. If I use something fromanimals
I don't get any suggestions. To get them I need to have somewhere inside myzoo
project already defined at least one import fromanimals
project. Now the suggestions show wrong paths. Vscode tells me to use the imports that point to myoutDir
folder which islib
, example:import { createDog } from '@app/../../lib/animals';
orimport { createDog } from '../../lib/animals';
Correct suggestions (code compiles with them):import { createDog } from '@animals/index'
import { createDog } from '../../animals
So here are three problems.
references
entry from tsconfig?@animals
path alias