How can I avoid the "an import path cannot end with .ts extension" error in VSCode?
Asked Answered
S

2

36

I'm using Visual Studio Code to write Deno programs.

The import statement in Deno contains the .ts extension, e.g.

import { Application } from "https://deno.land/x/oak/mod.ts";

which is marked as a problem in VS-Code, error code ts(2691):

enter image description here

Despite the shown error, the program works fine, but I want to avoid the error message, because there's nothing wrong.

How can this be solved?

Sinkage answered 2/12, 2020 at 20:17 Comment(0)
S
50

This can be solved by installing and configuring the Deno Extensions for VSCode.

Press CtrlShiftX to open the extensions view, then type "deno" and click on the entry name "Deno - Deno support for VSCode":

enter image description here

and install it.

After installation, you can choose any one of following methods:

  • Go to settings: (Ctrl, or Cmd, on MacOSX), select "Extensions>Deno" and click on the checkbox under "Deno:Enable" and "Deno:lint"
    • Just enable workspace settings only
    • Don't enable User settings if you have other Non node projects

enter image description here enter image description here

  • Or, as @hong4rc mentioned, open the Command Palette(Ctrl+Shift+P) and select Deno:initialize

Either method creates a folder named .vscode and a file settings.json with the following lines in your workspace project ( workspace settings):

{
  "deno.enable": true,
  "deno.lint": true
}

In both cases, after restarting Visual Studio Code, the error should be gone.

Sinkage answered 2/12, 2020 at 20:17 Comment(7)
You can run Ctrl+Shift+P and type deno init (it auto enable another setting, just remove if you want)Rompers
@Rompers thanks, so you can switch on nd off the extension with a command.I tried but got an error message "Init failed, Source: Deno (Extension)". Any idea why?Sinkage
Try this: disable tslint(if you installed), reopen VScode, check your project in the normal disk (!= C)Rompers
Wow! Works just as expected!Simons
this does not resolve it for me.Gallice
As @Rompers mentioned, Ctrl+Shift+P and type deno: initialize workspace resolved for me.Vicechairman
I was able to resolve this by disabling typescript.tsserver.experimental.enableProjectDiagnostics in the workspace.Jurdi
H
7

Since this is the first question that pops up in search I will add my answer here although it was not related to Deno, but with Webdriver.

Adding allowImportingTsExtensions in tsconfig.json solved the issue.

My full answer here: https://mcmap.net/q/275674/-why-must-i-import-ts-files-as-js-files-in-this-case

Heteronym answered 25/5, 2023 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.