Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3de706a

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

2 files changed

+37Lines changed: 37 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/services/codefixes/importFixes.ts‎

Copy file name to clipboardExpand all lines: src/services/codefixes/importFixes.ts
+3Lines 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;
Collapse file
+34Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.