Skip to content

TypeScript prefers deep paths for import quick fixes (and auto-imports?) #40223

Closed
@jazzfog

Description

@jazzfog

TypeScript Version: 4.0.2

Search Terms: Import

According to the TypeScript 4 release notes auto-imports should be improved, it is the case now imports are getting added automatically but it seems like the paths are not correct - to /dist/ folder as opposed to what library is exposing.

This won't work in prod-ish environment, Node.js might not be able to resolve such paths. In the example below - paths are pointing at the d.ts files that has not meaning for Node.js

  • This is the case for both single-import and "Add all missing imports" options.
  • This happens during only the initial import, once the library imported once in a correct way - all dependencies are getting imported with correct paths.

Please see the example and comments:

Code

Following is a file from an app generated by create-react-app, TypeScript was manually updated to 4.0.2, also added react-hook-form dependency.

import React, { ReactNode } from 'react';

// TypeScript 4 adds incorrect dependencies

// Correct imports, added manually
// ---
// import { useFieldArray, useForm } from "react-hook-form";

// Wrong imports (added by TypeScript 4 and VSCode),
// Node is not able to use these paths since they are pointing at d.ts files.
// ---
// import { useFieldArray } from 'react-hook-form/dist/useFieldArray';
// import { useForm } from 'react-hook-form/dist/useForm';

function App() {

  // Press `Ctrl + .` (Windows) or `Cmd + .` (macOS)
  // 
  // console.log(useForm, useFieldArray);

  return <>...</>;
}

export default App;

Expected behavior: Imports should point at the compiled code

Actual behavior: Imports point at the original TS code that node won't be able to execute once the project is compiled.

Playground Link: GitHub repo with example

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Quick FixesEditor-provided fixes, often called code actions.Fix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions