typescript auto imports not working macos
Asked Answered
G

2

7

I have a project and I am adding some typescript but typescript doesnt auto import. And i dont understand why. See below my file structure, tsconfig and an example:

ts config

{ 
    "compilerOptions": { 
        "target": "es6",
        "module": "commonjs",
        "declaration": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "sourceMap": true,
        "pretty": true,
        "allowUnreachableCode": false,
        "allowUnusedLabels": false,
        "noImplicitAny": true,
        "noImplicitReturns": false,
        "noImplicitUseStrict": false,
        "outDir": "../Js/",
        "baseUrl": "./",
    },
    "include":[ 
       "*.ts"
    ],
    "compileOnSave": true
}

file structure
enter image description here

App ts expected import suggestion
enter image description here Here I am expecting to see a suggestion for an import for ImageRowsInitializer from the file called images-row.ts.

images-row.ts

export class ImageRowsInitializer {
    
    private image_rows : ImagesRow[];
    
    constructor() {
        const htmlImageRows = document.getElementsByClassName("row-container");
        for (let i = 0; i < htmlImageRows.length; i++) {
            const imagerow = htmlImageRows[i];
            this.image_rows.push(new ImagesRow(imagerow as HTMLElement));
        }
    }
}

I dont get why i am not getting suggestions..
please let me know if more information is needed i am happy to supply :)

Gagliardi answered 23/6, 2020 at 17:16 Comment(0)
A
21

Click on TypeScript version in the lower-right corner of VSCode. enter image description here

Now from the command section, select workspace typescript version.

enter image description here

if the error is still not fixed, then - go to settings, Search for "tsdk" click on edit in settings.json and delete the property "typescript.tsdk": "node_modules\\typescript\\lib"

The Third Option is to include all the files under your "Typescript" folder. In your case, typescript is unable to locate all the files in your project. To locate all the files, modify your include array in tsconfig to "include":[ "**/*" ]. It will inform the TS compiler of VSCode to search all the ts files under "Typescript" folder.

This may fix your auto import issue.

enter image description here

Anchorage answered 23/6, 2020 at 18:16 Comment(7)
for me the use workspace version doesnt exist. Could it be because i have typescript installed globally?Gagliardi
Yes. It may be the reason. Try the second fix. go to settings => search for 'tsdk' => click on edit in settings.json => delete "typescript.tsdk" property.Anchorage
Didnt fix it :(Gagliardi
I created a setup/folder-structure as you mentioned in your question. Then I delete this from tsconfig.json - "include":[ "*.ts" ], and auto-import works for me.Anchorage
awesome! that fixed it, any clue why this happens? I am a newb to typescript.Gagliardi
That include array informs typescript compiler that it has to look into those paths which are mentioned in include array. So, if you change your include array to "include":[ "**/*" ] , it will include all the files where tsconfig is defined(in your case it will look for all the files under "Typescript" folder. just change the include to "include":[ "**/*" ]Anchorage
thanks for the explanation. If you add it to your answer i will accept it :)Gagliardi
L
3

Select TypeScript Version to be your workspace version, not the vscode version solved the issue on my end. Not sure why though.

enter image description here

Lathrope answered 3/3, 2023 at 3:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.