organize-imports-cli icon indicating copy to clipboard operation
organize-imports-cli copied to clipboard

Bug report: `organize-imports` strips type imports that only appear in `satisfies` right-hand side in typescript project

Open thetos7 opened this issue 1 year ago • 2 comments

I have noticed this issue recently in projects.

In TypeScript files, whenever a type from a different module is exclusively used in satsifies type assertions, the organize-imports CLI strips the type from the import, resulting in the file having compilation errors.

Example

types.ts

export type Content = {
  text: string;
};

export const buildContent = (arg: Content) => {
  // implementation
};

main.ts

import { Content, buildContent } from "./types";

buildContent({
  text: "my text"
} satisfies Content);

When running organize-imports, I would expect main.ts to remain the same, however the output ends up being:

import { buildContent } from "./types";

buildContent({
  text: "my text"
} satisfies Content); // ERROR: Content is not defined!

Which is a compile-time error because of the missing type identifier.

Workaround

I do have a workaround for now, by referencing the type in a type alias:

import { Content, buildContent } from "./types";

type __Keep = Content;

buildContent({
  text: "my text"
} satisfies Content);

thetos7 avatar Jul 27 '23 15:07 thetos7

Should be fixed by bumping ts-morph to the current version. @thorn0 Is the project still alive?

lmexo avatar Aug 03 '23 07:08 lmexo

I worked around this error with yarn resolutions (overrides for npm). But a new release would be good 👍

jakkn avatar Apr 16 '24 12:04 jakkn