Skip to content

Fix isolatedModules check for export assignments #57148

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 1 commit into from
Jan 24, 2024
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
7 changes: 4 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46384,11 +46384,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
);
}

if (!isIllegalExportDefaultInCJS && getIsolatedModules(compilerOptions) && !(sym.flags & SymbolFlags.Value)) {
if (!isIllegalExportDefaultInCJS && !(node.flags & NodeFlags.Ambient) && getIsolatedModules(compilerOptions) && !(sym.flags & SymbolFlags.Value)) {
const nonLocalMeanings = getSymbolFlags(sym, /*excludeTypeOnlyMeanings*/ false, /*excludeLocalMeanings*/ true);
if (
sym.flags & SymbolFlags.Alias
&& resolveAlias(sym) !== unknownSymbol
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unknownSymbol.flags === SymbolFlags.All, so while this check was necessary before, it’s now redundant with !(nonLocalMeanings & SymbolFlags.Value).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're actually equal to All? I thought that it was SymbolFlags.Property for this symbol.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, oops, you’re right. At any rate, it’s still redundant with !(nonLocalMeanings & SymbolFlags.Value), but I’m glad to fix that memory

&& getSymbolFlags(sym, /*excludeTypeOnlyMeanings*/ false, /*excludeLocalMeanings*/ true) & SymbolFlags.Type
&& nonLocalMeanings & SymbolFlags.Type
&& !(nonLocalMeanings & SymbolFlags.Value)
&& (!typeOnlyDeclaration || getSourceFileOfNode(typeOnlyDeclaration) !== getSourceFileOfNode(node))
) {
// import { SomeType } from "./someModule";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/b.ts(3,5): error TS1362: 'A' cannot be used as a value because it was exported using 'export type'.
/d.ts(2,10): error TS1291: 'A' resolves to a type and must be marked type-only in this file before re-exporting when 'isolatedModules' is enabled. Consider using 'import type' where 'A' is imported.
/d.ts(2,10): error TS1289: 'A' resolves to a type-only declaration and must be marked type-only in this file before re-exporting when 'isolatedModules' is enabled. Consider using 'import type' where 'A' is imported.


==== /a.ts (0 errors) ====
Expand All @@ -22,4 +22,5 @@
import { A } from './a';
export = A;
~
!!! error TS1291: 'A' resolves to a type and must be marked type-only in this file before re-exporting when 'isolatedModules' is enabled. Consider using 'import type' where 'A' is imported.
!!! error TS1289: 'A' resolves to a type-only declaration and must be marked type-only in this file before re-exporting when 'isolatedModules' is enabled. Consider using 'import type' where 'A' is imported.
!!! related TS1377 /a.ts:2:15: 'A' was exported here.
25 changes: 25 additions & 0 deletions tests/cases/compiler/isolatedModulesReExportAlias.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// @isolatedModules: true
// @noEmit: true
// @noTypesAndSymbols: true

// @Filename: /events.d.ts
declare module "events" {
interface EventEmitterOptions {
captureRejections?: boolean;
}
class EventEmitter {
constructor(options?: EventEmitterOptions);
}
export = EventEmitter;
}
declare module "node:events" {
import events = require("events");
export = events;
}

// @Filename: /moreEvents.ts
import events = require("events");
export = events;

// @Filename: /boo.ts
// Bad test runner (ignores stuff when last file contains the string r-e-q-u-i-r-e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am definitely curious about this one... 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The absolute worst

// We need to assemble the list of input files for the compiler and other related files on the 'filesystem' (ie in a multi-file test)
// If the last file in a test uses require or a triple slash reference we'll assume all other files will be brought in via references,
// otherwise, assume all files are just meant to be in the same compilation session without explicit references to one another.
if (testCaseContent.settings.noImplicitReferences || /require\(/.test(lastUnit.content) || /reference\spath/.test(lastUnit.content)) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this is bogus