Skip to content

typescript.preferences.preferTypeOnlyAutoImports not working with TypeScript 5.3? #56832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
johnnyreilly opened this issue Dec 19, 2023 · 3 comments · Fixed by microsoft/vscode#201376
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@johnnyreilly
Copy link

johnnyreilly commented Dec 19, 2023

🔎 Search Terms

preferTypeOnlyAutoImports

🕗 Version & Regression Information

I was very excited about:

With a recent change, TypeScript now enables this to be an editor-specific option. In Visual Studio Code, you can enable it in the UI under "TypeScript › Preferences: Prefer Type Only Auto Imports", or as the JSON configuration option typescript.preferences.preferTypeOnlyAutoImports

But when I came to take it for a spin with [email protected] the original import behaviour (i.e. importing without a type specifier) occurred.

⏯ Playground Link

No response

💻 Code

Before auto imports kicked in:

import { useAssistants } from "../data";

🙁 Actual behavior

import { QueryOrView, useAssistants } from "../data";

The QueryOrView type import above was added by the auto-imports behaviour of VS Code despite having typescript.preferences.preferTypeOnlyAutoImports: true

🙂 Expected behavior

import { type QueryOrView, useAssistants } from "../data";

I would expect the above as the the auto-imports behaviour of VS Code due to having typescript.preferences.preferTypeOnlyAutoImports: true

Additional information about the issue

My tsconfig.json in case it is relevant:

{
    "compilerOptions": {
        "target": "ESNext",
        "useDefineForClassFields": true,
        "lib": ["DOM", "DOM.Iterable", "ESNext"],
        "allowJs": false,
        "skipLibCheck": true,
        "esModuleInterop": false,
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "module": "ESNext",
        "moduleResolution": "Node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "noEmit": true,
        "jsx": "react-jsx",
        "noErrorTruncation": true
    },
    "include": ["src"],
    "references": [{ "path": "./tsconfig.node.json" }]
}

where tsconfig.node.json is

{
    "compilerOptions": {
        "composite": true,
        "module": "esnext",
        "moduleResolution": "node"
    },
    "include": ["vite.config.ts"]
}

(A basic Vite generated tsconfig.json)

My .eslintrc.cjs (not sure this is actually relevant)

// eslint-disable-next-line no-undef
module.exports = {
    root: true,
    parser: "@typescript-eslint/parser",
    plugins: ["@typescript-eslint"],
    extends: [
        "eslint:recommended",
        "plugin:@typescript-eslint/recommended",
        "plugin:react/recommended",
        "plugin:react-hooks/recommended",
        "prettier",
        "plugin:storybook/recommended",
    ],
    rules: {
        "react/jsx-uses-react": "off",
        "react/react-in-jsx-scope": "off",
        "@typescript-eslint/consistent-type-imports": "error", // THIS SHOUTS AT ME AND I LOVE IT
    },
    settings: {
        react: {
            version: "detect",
        },
    },
    ignorePatterns: ["dist/"],
};

I'm wondering if I'm making a silly mistake, or if I should be modifying the advice in my blog post in some way. Not sure.

https://johnnyreilly.com/typescript-5-importsnotusedasvalues-error-eslint-consistent-type-imports#make-vs-code-prefer-type-imports

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Dec 19, 2023
@andrewbranch andrewbranch added this to the TypeScript 5.4.0 milestone Dec 20, 2023
@weyert
Copy link

weyert commented Dec 20, 2023

Wouldn't you expect the result to be:

import {useAssistants } from "../data";
import { type QueryOrView } from "../data";

@ethanresnick
Copy link
Contributor

I had a similar issue earlier. When I investigated, it seemed like there were changes needed in VS Code itself to make this feature work, and those changes hadn't been released yet. But I'm not sure if that still explains it (and it's possible that that was never the explanation).

@andrewbranch
Copy link
Member

Workaround until microsoft/vscode#201376 is merged/released: you can set this in your .vscode/settings.json:

{
  "typescript.preferTypeOnlyAutoImports": true,
}

VS Code is accidentally pulling from that instead of typescript.preferences.preferTypeOnlyAutoImports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants