Skip to content

Commit 3de706a

Browse files
authored
Don’t create invalid type-only imports during add missing import (#43828)
1 parent d5af89c commit 3de706a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/services/codefixes/importFixes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,9 @@ namespace ts.codefix {
420420
const { importClause } = declaration;
421421
if (!importClause || !isStringLiteralLike(declaration.moduleSpecifier)) return undefined;
422422
const { name, namedBindings } = importClause;
423+
// A type-only import may not have both a default and named imports, so the only way a name can
424+
// be added to an existing type-only import is adding a named import to existing named bindings.
425+
if (importClause.isTypeOnly && !(importKind === ImportKind.Named && namedBindings)) return undefined;
423426
return importKind === ImportKind.Default && !name || importKind === ImportKind.Named && (!namedBindings || namedBindings.kind === SyntaxKind.NamedImports)
424427
? { kind: ImportFixKind.AddToExisting, importClauseOrBindingPattern: importClause, importKind, moduleSpecifier: declaration.moduleSpecifier.text, canUseTypeOnlyImport }
425428
: undefined;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @importsNotUsedAsValues: error
4+
5+
// @Filename: Presenter.ts
6+
//// export type DisplayStyle = "normal" | "compact";
7+
////
8+
//// export default class Presenter {
9+
//// present(displayStyle: DisplayStyle): Element {
10+
//// return document.createElement("placeholder");
11+
//// }
12+
//// }
13+
14+
// @Filename: index.ts
15+
//// import type Presenter from "./Presenter";
16+
////
17+
//// function present(
18+
//// presenter: Presenter,
19+
//// displayStyle: DisplayStyle,
20+
//// ) {}
21+
22+
goTo.file("index.ts");
23+
verify.codeFix({
24+
errorCode: ts.Diagnostics.Cannot_find_name_0.code,
25+
description: ignoreInterpolations(ts.Diagnostics.Import_0_from_module_1),
26+
newFileContent:
27+
`import type { DisplayStyle } from "./Presenter";
28+
import type Presenter from "./Presenter";
29+
30+
function present(
31+
presenter: Presenter,
32+
displayStyle: DisplayStyle,
33+
) {}`
34+
});

0 commit comments

Comments
 (0)