Skip to content

Fix export * that resolves to something type-only #36558

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

Merged
merged 2 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2355,7 +2355,10 @@ namespace ts {

function getTargetOfNamespaceExport(node: NamespaceExport, dontResolveAlias: boolean): Symbol | undefined {
const moduleSpecifier = node.parent.moduleSpecifier;
return moduleSpecifier && resolveESModuleSymbol(resolveExternalModuleName(node, moduleSpecifier), moduleSpecifier, dontResolveAlias, /*suppressUsageError*/ false);
const immediate = moduleSpecifier && resolveExternalModuleName(node, moduleSpecifier);
const resolved = moduleSpecifier && resolveESModuleSymbol(immediate, moduleSpecifier, dontResolveAlias, /*suppressUsageError*/ false);
markSymbolOfAliasDeclarationIfTypeOnly(node, immediate, resolved, /*overwriteEmpty*/ false);
return resolved;
}

// This function creates a synthetic symbol that combines the value side of one symbol with the
Expand Down Expand Up @@ -2504,7 +2507,9 @@ namespace ts {
}

function getTargetOfNamespaceExportDeclaration(node: NamespaceExportDeclaration, dontResolveAlias: boolean): Symbol {
return resolveExternalModuleSymbol(node.parent.symbol, dontResolveAlias);
const resolved = resolveExternalModuleSymbol(node.parent.symbol, dontResolveAlias);
markSymbolOfAliasDeclarationIfTypeOnly(node, /*immediateTarget*/ undefined, resolved, /*overwriteEmpty*/ false);
return resolved;
}

function getTargetOfExportSpecifier(node: ExportSpecifier, meaning: SymbolFlags, dontResolveAlias?: boolean) {
Expand Down
82 changes: 71 additions & 11 deletions src/testRunner/unittests/tsserver/typeOnlyImportChains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace ts.projectSystem {
content: "import { a } from './b'; new a.A();"
};

assertUsageError([a, b, c], c);
assertUsageError([a, b, c], c, Diagnostics._0_cannot_be_used_as_a_value_because_it_was_imported_using_import_type);
});

it("named export -> type-only named import -> named export -> named import", () => {
Expand All @@ -31,7 +31,7 @@ namespace ts.projectSystem {
content: "import { A } from './b'; new A();"
};

assertUsageError([a, b, c], c);
assertUsageError([a, b, c], c, Diagnostics._0_cannot_be_used_as_a_value_because_it_was_imported_using_import_type);
});

it("named export -> type-only namespace import -> export equals -> import equals", () => {
Expand All @@ -48,7 +48,7 @@ namespace ts.projectSystem {
content: "import a = require('./b'); new a.A();"
};

assertUsageError([a, b, c], c);
assertUsageError([a, b, c], c, Diagnostics._0_cannot_be_used_as_a_value_because_it_was_imported_using_import_type);
});

it("named export -> type-only namespace import -> export default -> import default", () => {
Expand All @@ -65,13 +65,13 @@ namespace ts.projectSystem {
content: "import a from './b'; new a.A();"
};

assertUsageError([a, b, c], c);
assertUsageError([a, b, c], c, Diagnostics._0_cannot_be_used_as_a_value_because_it_was_imported_using_import_type);
});

it("export default -> type-only import default -> export default -> import default", () => {
const a = {
path: "/a.ts",
content: "export defai;t class A {}"
content: "export default class A {}"
};
const b = {
path: "/b.ts",
Expand All @@ -82,11 +82,74 @@ namespace ts.projectSystem {
content: "import A from './b'; new A();"
};

assertUsageError([a, b, c], c);
assertUsageError([a, b, c], c, Diagnostics._0_cannot_be_used_as_a_value_because_it_was_imported_using_import_type);
});

it("named export -> type-only export from -> export star from -> named import", () => {
const a = {
path: "/a.ts",
content: "export class A {}"
};
const b = {
path: "/b.ts",
content: "export type { A } from './a';"
};
const c = {
path: "/c.ts",
content: "export * from './b';"
};
const d = {
path: "/d.ts",
content: "import { A } from './c'; new A();"
};

assertUsageError([a, b, c, d], d, Diagnostics._0_cannot_be_used_as_a_value_because_it_was_exported_using_export_type);
});

it("named export -> export namespace from -> type-only named import -> named export -> named import", () => {
const a = {
path: "/a.ts",
content: "export class A {}"
};
const b = {
path: "/b.ts",
content: "export * as a from './a';"
};
const c = {
path: "/c.ts",
content: "import type { a } from './b'; export { a };"
};
const d = {
path: "/d.ts",
content: "import { a } from './c'; new a.A();"
};

assertUsageError([a, b, c, d], d, Diagnostics._0_cannot_be_used_as_a_value_because_it_was_imported_using_import_type);
});

it("named export -> type-only export from -> export namespace from -> named import", () => {
const a = {
path: "/a.ts",
content: "export class A {}"
};
const b = {
path: "/b.ts",
content: "export type { A } from './a';"
};
const c = {
path: "/c.ts",
content: "export * as a from './b';"
};
const d = {
path: "/d.ts",
content: "import { a } from './c'; new a.A();"
};

assertUsageError([a, b, c, d], d, Diagnostics.Property_0_does_not_exist_on_type_1);
});
});

function assertUsageError(files: readonly TestFSWithWatch.File[], openFile: TestFSWithWatch.File) {
function assertUsageError(files: readonly TestFSWithWatch.File[], openFile: TestFSWithWatch.File, diagnostic: DiagnosticMessage) {
const host = createServerHost(files);
const session = createSession(host);
openFilesForSession([openFile], session);
Expand All @@ -96,9 +159,6 @@ namespace ts.projectSystem {
);
const diagnostics = session.executeCommand(req).response as protocol.Diagnostic[];
assert.lengthOf(diagnostics, 1);
assert.oneOf(diagnostics[0].code, [
Diagnostics._0_cannot_be_used_as_a_value_because_it_was_imported_using_import_type.code,
Diagnostics._0_cannot_be_used_as_a_value_because_it_was_exported_using_export_type.code
]);
assert.equal(diagnostics[0].code, diagnostic.code);
}
}
19 changes: 19 additions & 0 deletions tests/baselines/reference/exportNamespace1.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
tests/cases/conformance/externalModules/typeOnly/d.ts(2,5): error TS1362: 'A' cannot be used as a value because it was exported using 'export type'.


==== tests/cases/conformance/externalModules/typeOnly/a.ts (0 errors) ====
export class A {}

==== tests/cases/conformance/externalModules/typeOnly/b.ts (0 errors) ====
export type { A } from './a';

==== tests/cases/conformance/externalModules/typeOnly/c.ts (0 errors) ====
export * from './b';

==== tests/cases/conformance/externalModules/typeOnly/d.ts (1 errors) ====
import { A } from './c';
new A(); // Error
~
!!! error TS1362: 'A' cannot be used as a value because it was exported using 'export type'.
!!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:1:15: 'A' was exported here.

39 changes: 39 additions & 0 deletions tests/baselines/reference/exportNamespace1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//// [tests/cases/conformance/externalModules/typeOnly/exportNamespace1.ts] ////

//// [a.ts]
export class A {}

//// [b.ts]
export type { A } from './a';

//// [c.ts]
export * from './b';

//// [d.ts]
import { A } from './c';
new A(); // Error


//// [a.js]
"use strict";
exports.__esModule = true;
var A = /** @class */ (function () {
function A() {
}
return A;
}());
exports.A = A;
//// [b.js]
"use strict";
exports.__esModule = true;
//// [c.js]
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
exports.__esModule = true;
__export(require("./b"));
//// [d.js]
"use strict";
exports.__esModule = true;
new A(); // Error
18 changes: 18 additions & 0 deletions tests/baselines/reference/exportNamespace1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/conformance/externalModules/typeOnly/a.ts ===
export class A {}
>A : Symbol(A, Decl(a.ts, 0, 0))

=== tests/cases/conformance/externalModules/typeOnly/b.ts ===
export type { A } from './a';
>A : Symbol(A, Decl(b.ts, 0, 13))

=== tests/cases/conformance/externalModules/typeOnly/c.ts ===
export * from './b';
No type information for this code.
No type information for this code.=== tests/cases/conformance/externalModules/typeOnly/d.ts ===
import { A } from './c';
>A : Symbol(A, Decl(d.ts, 0, 8))

new A(); // Error
>A : Symbol(A, Decl(d.ts, 0, 8))

19 changes: 19 additions & 0 deletions tests/baselines/reference/exportNamespace1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/conformance/externalModules/typeOnly/a.ts ===
export class A {}
>A : A

=== tests/cases/conformance/externalModules/typeOnly/b.ts ===
export type { A } from './a';
>A : import("tests/cases/conformance/externalModules/typeOnly/a").A

=== tests/cases/conformance/externalModules/typeOnly/c.ts ===
export * from './b';
No type information for this code.
No type information for this code.=== tests/cases/conformance/externalModules/typeOnly/d.ts ===
import { A } from './c';
>A : typeof A

new A(); // Error
>new A() : A
>A : typeof A

20 changes: 20 additions & 0 deletions tests/baselines/reference/exportNamespace2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
tests/cases/conformance/externalModules/typeOnly/d.ts(2,5): error TS1361: 'a' cannot be used as a value because it was imported using 'import type'.


==== tests/cases/conformance/externalModules/typeOnly/a.ts (0 errors) ====
export class A {}

==== tests/cases/conformance/externalModules/typeOnly/b.ts (0 errors) ====
export * as a from './a';

==== tests/cases/conformance/externalModules/typeOnly/c.ts (0 errors) ====
import type { a } from './b';
export { a };

==== tests/cases/conformance/externalModules/typeOnly/d.ts (1 errors) ====
import { a } from './c';
new a.A(); // Error
~
!!! error TS1361: 'a' cannot be used as a value because it was imported using 'import type'.
!!! related TS1376 tests/cases/conformance/externalModules/typeOnly/c.ts:1:15: 'a' was imported here.

37 changes: 37 additions & 0 deletions tests/baselines/reference/exportNamespace2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//// [tests/cases/conformance/externalModules/typeOnly/exportNamespace2.ts] ////

//// [a.ts]
export class A {}

//// [b.ts]
export * as a from './a';

//// [c.ts]
import type { a } from './b';
export { a };

//// [d.ts]
import { a } from './c';
new a.A(); // Error


//// [a.js]
"use strict";
exports.__esModule = true;
var A = /** @class */ (function () {
function A() {
}
return A;
}());
exports.A = A;
//// [b.js]
"use strict";
exports.__esModule = true;
exports.a = require("./a");
//// [c.js]
"use strict";
exports.__esModule = true;
//// [d.js]
"use strict";
exports.__esModule = true;
new a.A(); // Error
24 changes: 24 additions & 0 deletions tests/baselines/reference/exportNamespace2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/conformance/externalModules/typeOnly/a.ts ===
export class A {}
>A : Symbol(A, Decl(a.ts, 0, 0))

=== tests/cases/conformance/externalModules/typeOnly/b.ts ===
export * as a from './a';
>a : Symbol(a, Decl(b.ts, 0, 11))

=== tests/cases/conformance/externalModules/typeOnly/c.ts ===
import type { a } from './b';
>a : Symbol(a, Decl(c.ts, 0, 13))

export { a };
>a : Symbol(a, Decl(c.ts, 1, 8))

=== tests/cases/conformance/externalModules/typeOnly/d.ts ===
import { a } from './c';
>a : Symbol(a, Decl(d.ts, 0, 8))

new a.A(); // Error
>a.A : Symbol(a.A, Decl(a.ts, 0, 0))
>a : Symbol(a, Decl(d.ts, 0, 8))
>A : Symbol(a.A, Decl(a.ts, 0, 0))

25 changes: 25 additions & 0 deletions tests/baselines/reference/exportNamespace2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/conformance/externalModules/typeOnly/a.ts ===
export class A {}
>A : A

=== tests/cases/conformance/externalModules/typeOnly/b.ts ===
export * as a from './a';
>a : typeof a

=== tests/cases/conformance/externalModules/typeOnly/c.ts ===
import type { a } from './b';
>a : any

export { a };
>a : typeof a

=== tests/cases/conformance/externalModules/typeOnly/d.ts ===
import { a } from './c';
>a : typeof a

new a.A(); // Error
>new a.A() : a.A
>a.A : typeof a.A
>a : typeof a
>A : typeof a.A

18 changes: 18 additions & 0 deletions tests/baselines/reference/exportNamespace3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
tests/cases/conformance/externalModules/typeOnly/d.ts(2,7): error TS2339: Property 'A' does not exist on type 'typeof import("tests/cases/conformance/externalModules/typeOnly/b")'.


==== tests/cases/conformance/externalModules/typeOnly/a.ts (0 errors) ====
export class A {}

==== tests/cases/conformance/externalModules/typeOnly/b.ts (0 errors) ====
export type { A } from './a';

==== tests/cases/conformance/externalModules/typeOnly/c.ts (0 errors) ====
export * as a from './b';

==== tests/cases/conformance/externalModules/typeOnly/d.ts (1 errors) ====
import { a } from './c';
new a.A(); // Error
~
!!! error TS2339: Property 'A' does not exist on type 'typeof import("tests/cases/conformance/externalModules/typeOnly/b")'.

Loading