Skip to content

Type-only imports and exports #35200

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 50 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
426685a
Add type-only support for export declarations
andrewbranch Nov 12, 2019
856fe0f
Use a synthetic type alias instead of binding type-only exports as a …
andrewbranch Nov 13, 2019
9c659f6
Works for re-exports!
andrewbranch Nov 13, 2019
a3d16f6
isolatedModules works fine
andrewbranch Nov 13, 2019
edfc798
Diagnostic for type-only exporting a value
andrewbranch Nov 13, 2019
84238f9
Start isolated modules codefix
andrewbranch Nov 14, 2019
5d2bc5d
Update for LKG control flow changes
andrewbranch Nov 15, 2019
e736045
Type-only import clause parsing
andrewbranch Nov 18, 2019
558dd49
Type-only default import checking
andrewbranch Nov 18, 2019
151a397
Type-only named imports
andrewbranch Nov 18, 2019
85390d7
Fix isolated modules error
andrewbranch Nov 18, 2019
e130453
Filter namespaces down to type-only
andrewbranch Nov 18, 2019
1db2773
Fix class references
andrewbranch Nov 19, 2019
bd73f27
Test nested namespaces
andrewbranch Nov 19, 2019
fa72cf5
Test circular type-only imports/exports
andrewbranch Nov 19, 2019
6de5150
Fix getTypeAtLocation for type-only import/export specifiers
andrewbranch Nov 20, 2019
9bf28fe
Fix type-only generic imports
andrewbranch Nov 20, 2019
ed5dcf4
Update public APIs
andrewbranch Nov 20, 2019
3b45e98
Remove unused WIP comment
andrewbranch Nov 20, 2019
48de63b
Type-only namespace imports
andrewbranch Nov 20, 2019
113823d
Fix factory update calls
andrewbranch Nov 20, 2019
d69eed0
Add grammar errors for JS usage and mixing default and named bindings
andrewbranch Nov 21, 2019
c048c9a
Update updateExportDeclaration API baseline
andrewbranch Nov 21, 2019
2227f4e
Fix grammar checking import clauses
andrewbranch Nov 21, 2019
421d333
Enums, sort of
andrewbranch Nov 21, 2019
26704c0
Dedicated error for type-only enum
andrewbranch Nov 21, 2019
e8d4c08
Skip past type-only alias symbols in quick info
andrewbranch Nov 22, 2019
1c10aa1
Update error code in baseline
andrewbranch Nov 22, 2019
16c2534
WIP: convertToTypeOnlyExport
andrewbranch Nov 22, 2019
debc8cb
isolatedModules codefix (single export declaration)
andrewbranch Nov 22, 2019
ae00f75
isolatedModules code fix (all)
andrewbranch Nov 22, 2019
f823bf5
Stop eliding non-type-only imports by default, add compiler flag
andrewbranch Nov 25, 2019
12bd78d
Update to match updated diagnostic messages
andrewbranch Nov 25, 2019
3724f88
Update more baselines
andrewbranch Nov 25, 2019
42fc34a
Update more tests
andrewbranch Nov 25, 2019
73cd8ab
Auto-import as type-only
andrewbranch Nov 25, 2019
f846ff1
Add codefix for splitting type-only import with default and named bin…
andrewbranch Nov 26, 2019
b64effa
Add more services tests
andrewbranch Nov 26, 2019
010273a
Add targeted error message for "export type T;" when T exists
andrewbranch Nov 26, 2019
4992945
Add targeted error for "import type T = require(...)"
andrewbranch Nov 26, 2019
9490df5
Merge branch 'master' into feature/type-only
andrewbranch Dec 2, 2019
a2bdecf
Merge branch 'master' into feature/type-only
andrewbranch Dec 20, 2019
f7e889b
Flip emit flag
andrewbranch Dec 20, 2019
0c2df8e
Add test for preserveUnusedImports option
andrewbranch Dec 20, 2019
9b94bf9
Fix flag flip on import =
andrewbranch Dec 20, 2019
ae78865
Make compiler option string-valued
andrewbranch Dec 31, 2019
6b548bb
Merge branch 'master' into feature/type-only
andrewbranch Dec 31, 2019
8b279a9
Fix merge conflicts
andrewbranch Dec 31, 2019
f8333d0
Add --importsNotUsedAsValue=error
andrewbranch Dec 31, 2019
c71d87b
Phrasing of messages.
DanielRosenwasser Jan 3, 2020
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
Prev Previous commit
Next Next commit
Type-only default import checking
  • Loading branch information
andrewbranch committed Nov 25, 2019
commit 558dd49fda8c145c8a3b3f164e3fc9e02e6d089c
20 changes: 15 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2210,9 +2210,18 @@ namespace ts {
}
else if (hasSyntheticDefault) {
// per emit behavior, a synthetic default overrides a "real" .default member if `__esModule` is not present
return resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias) || resolveSymbol(moduleSymbol, dontResolveAlias);
return maybeTypeOnly(
resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias) ||
resolveSymbol(moduleSymbol, dontResolveAlias));
}
return exportDefaultSymbol;
return maybeTypeOnly(exportDefaultSymbol);
}

function maybeTypeOnly(symbol: Symbol | undefined) {
if (symbol && node.isTypeOnly && node.name) {
return createSyntheticTypeAlias(node.name, symbol);
}
return symbol;
}
}

Expand Down Expand Up @@ -2345,12 +2354,13 @@ namespace ts {
* `TypeFlags.Alias` so that alias resolution works as usual, but once the target `A`
* has been resolved, we essentially want to pretend we have a type alias to that target.
*/
function createSyntheticTypeAlias(sourceNode: ExportSpecifier | ImportSpecifier, target: Symbol) {
function createSyntheticTypeAlias(sourceNode: ExportSpecifier | Identifier, target: Symbol) {
if (!(target.flags & SymbolFlags.Type)) {
const nameText = idText(sourceNode.name);
const identifier = isExportSpecifier(sourceNode) ? sourceNode.name : sourceNode;
const nameText = idText(identifier);
addRelatedInfo(
error(
sourceNode.name,
identifier,
Diagnostics.Type_only_0_must_be_a_type_but_1_is_a_value,
isExportSpecifier(sourceNode) ? "export" : "import",
nameText),
Expand Down
13 changes: 13 additions & 0 deletions tests/baselines/reference/importClause_default.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/b.ts(2,5): error TS2693: 'A' only refers to a type, but is being used as a value here.


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

==== /b.ts (1 errors) ====
import type A from './a';
new A();
~
!!! error TS2693: 'A' only refers to a type, but is being used as a value here.
let a: A;

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

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

//// [b.ts]
import type A from './a';
new A();
let a: A;


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

=== /b.ts ===
import type A from './a';
>A : Symbol(A, Decl(b.ts, 0, 6))

new A();
let a: A;
>a : Symbol(a, Decl(b.ts, 2, 3))
>A : Symbol(A, Decl(b.ts, 0, 6))

15 changes: 15 additions & 0 deletions tests/baselines/reference/importClause_default.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=== /a.ts ===
export default class A {}
>A : A

=== /b.ts ===
import type A from './a';
>A : any

new A();
>new A() : any
>A : any

let a: A;
>a : typeof import("/a").default

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy