Skip to content
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

Proposal: Always allow type-only imports to reference .ts extensions #54746

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -4905,8 +4905,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
else if (resolvedModule.resolvedUsingTsExtension && !shouldAllowImportingTsExtension(compilerOptions, currentSourceFile.fileName)) {
const tsExtension = Debug.checkDefined(tryExtractTSExtension(moduleReference));
error(errorNode, Diagnostics.An_import_path_can_only_end_with_a_0_extension_when_allowImportingTsExtensions_is_enabled, tsExtension);
const importOrExport =
findAncestor(location, isImportDeclaration)?.importClause ||
findAncestor(location, or(isImportEqualsDeclaration, isExportDeclaration));
if (!(importOrExport?.isTypeOnly || findAncestor(location, isImportTypeNode))) {
const tsExtension = Debug.checkDefined(tryExtractTSExtension(moduleReference));
error(errorNode, Diagnostics.An_import_path_can_only_end_with_a_0_extension_when_allowImportingTsExtensions_is_enabled, tsExtension);
}
}

if (sourceFile.symbol) {
Expand Down
21 changes: 21 additions & 0 deletions tests/baselines/reference/allowsImportingTsExtension.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
b.ts(2,16): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
b.ts(3,30): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
b.ts(5,25): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.


==== a.ts (0 errors) ====
export class A {}

==== b.ts (3 errors) ====
import type { A } from "./a.ts"; // ok
import {} from "./a.ts"; // error
~~~~~~~~
!!! error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
import { type A as _A } from "./a.ts"; // error
Copy link
Member

Choose a reason for hiding this comment

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

Interesting i would have expected above two to be not errors per your description since its not in emitted?

Copy link
Member Author

Choose a reason for hiding this comment

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

It’s a bit of a gray area, but I decided to treat this as if verbatimModuleSyntax is enabled. Depending on settings, this import could get preserved as import {} from "./a.ts", whereas import type is unambiguously elided. It feels better to rely on the strong, unambiguous signal that the import will always be elided.

~~~~~~~~
!!! error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
type __A = import("./a.ts").A; // ok
const aPromise = import("./a.ts"); // error
~~~~~~~~
!!! error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.

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

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

//// [b.ts]
import type { A } from "./a.ts"; // ok
import {} from "./a.ts"; // error
import { type A as _A } from "./a.ts"; // error
type __A = import("./a.ts").A; // ok
const aPromise = import("./a.ts"); // error


//// [a.js]
export class A {
}
//// [b.js]
const aPromise = import("./a.ts"); // error
export {};
23 changes: 23 additions & 0 deletions tests/baselines/reference/allowsImportingTsExtension.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [tests/cases/conformance/externalModules/typeOnly/allowsImportingTsExtension.ts] ////

=== a.ts ===
export class A {}
>A : Symbol(A, Decl(a.ts, 0, 0))

=== b.ts ===
import type { A } from "./a.ts"; // ok
>A : Symbol(A, Decl(b.ts, 0, 13))

import {} from "./a.ts"; // error
import { type A as _A } from "./a.ts"; // error
>A : Symbol(A, Decl(a.ts, 0, 0))
>_A : Symbol(_A, Decl(b.ts, 2, 8))

type __A = import("./a.ts").A; // ok
>__A : Symbol(__A, Decl(b.ts, 2, 38))
>A : Symbol(A, Decl(a.ts, 0, 0))

const aPromise = import("./a.ts"); // error
>aPromise : Symbol(aPromise, Decl(b.ts, 4, 5))
>"./a.ts" : Symbol("a", Decl(a.ts, 0, 0))

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

=== a.ts ===
export class A {}
>A : A

=== b.ts ===
import type { A } from "./a.ts"; // ok
>A : A

import {} from "./a.ts"; // error
import { type A as _A } from "./a.ts"; // error
>A : typeof A
>_A : typeof A

type __A = import("./a.ts").A; // ok
>__A : A

const aPromise = import("./a.ts"); // error
>aPromise : Promise<typeof import("a")>
>import("./a.ts") : Promise<typeof import("a")>
>"./a.ts" : "./a.ts"

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @allowImportingTsExtensions: false
// @target: esnext
// @module: esnext

// @Filename: a.ts

Choose a reason for hiding this comment

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

Is there already an equivalent test case of the 5 assertions below for the case where a.ts is a.d.ts?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think so, but I'll add one here so it can be compared easily

export class A {}

// @Filename: b.ts
import type { A } from "./a.ts"; // ok
import {} from "./a.ts"; // error
import { type A as _A } from "./a.ts"; // error
type __A = import("./a.ts").A; // ok
const aPromise = import("./a.ts"); // error