Skip to content

Commit c0831c2

Browse files
Merge pull request #121 from bloomberg/isolated-declarations-unused-imports
Revert change in checker that prevented TS from removing some imports
2 parents 4e01096 + cf1ca98 commit c0831c2

13 files changed

+398
-7
lines changed

src/compiler/checker.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,10 +2764,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
27642764
return symbol;
27652765
}
27662766
if (symbol.flags & SymbolFlags.Alias) {
2767-
// Do not take target symbol meaning into account in isolated declaration mode since we don't have access to info from other files.
2768-
if (compilerOptions.isolatedDeclarations) {
2769-
return symbol;
2770-
}
27712767
const targetFlags = getSymbolFlags(symbol);
27722768
// `targetFlags` will be `SymbolFlags.All` if an error occurred in alias resolution; this avoids cascading errors
27732769
if (targetFlags & meaning) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// [[Reason: TSC removes import that is not used in a semantically valid way. DTE can't know about this.]] ////
2+
3+
//// [tests/cases/compiler/decoratorMetadataWithImportDeclarationNameCollision7.ts] ////
4+
5+
===================================================================
6+
--- TSC declarations
7+
+++ DTE declarations
8+
@@ -5,8 +5,9 @@
9+
doSomething(): invalid;
10+
}
11+
12+
//// [service.d.ts]
13+
+import db from './db';
14+
declare class MyClass {
15+
db: db.db;
16+
constructor(db: db.db);
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// [[Reason: TSC removes type only import. DTE can't know import is type only.]] ////
2+
3+
//// [tests/cases/compiler/typeReferenceDirectives13.ts] ////
4+
5+
===================================================================
6+
--- TSC declarations
7+
+++ DTE declarations
8+
@@ -1,7 +1,8 @@
9+
10+
11+
//// [/app.d.ts]
12+
+import { $ } from "./ref";
13+
export interface A {
14+
x: () => typeof $;
15+
}
16+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// [[Reason: TSC removes type only import. DTE can't know import is type only.]] ////
2+
3+
//// [tests/cases/compiler/typeReferenceDirectives5.ts] ////
4+
5+
===================================================================
6+
--- TSC declarations
7+
+++ DTE declarations
8+
@@ -1,7 +1,8 @@
9+
10+
11+
//// [/app.d.ts]
12+
+import { $ } from "./ref";
13+
export interface A {
14+
x: typeof $;
15+
}
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//// [tests/cases/compiler/decoratorMetadataWithImportDeclarationNameCollision7.ts] ////
2+
3+
//// [db.ts]
4+
export default class db {
5+
public doSomething() {
6+
}
7+
}
8+
9+
//// [service.ts]
10+
import db from './db';
11+
function someDecorator(target) {
12+
return target;
13+
}
14+
@someDecorator
15+
class MyClass {
16+
db: db.db; //error
17+
18+
constructor(db: db.db) { // error
19+
this.db = db;
20+
this.db.doSomething();
21+
}
22+
}
23+
export {MyClass};
24+
25+
26+
/// [Declarations] ////
27+
28+
29+
30+
//// [db.d.ts]
31+
export default class db {
32+
doSomething(): invalid;
33+
}
34+
35+
//// [service.d.ts]
36+
import db from './db';
37+
declare class MyClass {
38+
db: db.db;
39+
constructor(db: db.db);
40+
}
41+
export { MyClass };
42+
43+
/// [Errors] ////
44+
45+
db.ts(2,12): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit.
46+
service.ts(7,9): error TS2702: 'db' only refers to a type, but is being used as a namespace here.
47+
service.ts(7,9): error TS4031: Public property 'db' of exported class has or is using private name 'db'.
48+
service.ts(9,21): error TS2702: 'db' only refers to a type, but is being used as a namespace here.
49+
service.ts(9,21): error TS4063: Parameter 'db' of constructor from exported class has or is using private name 'db'.
50+
51+
52+
==== db.ts (1 errors) ====
53+
export default class db {
54+
public doSomething() {
55+
~~~~~~~~~~~
56+
!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit.
57+
}
58+
}
59+
60+
==== service.ts (4 errors) ====
61+
import db from './db';
62+
function someDecorator(target) {
63+
return target;
64+
}
65+
@someDecorator
66+
class MyClass {
67+
db: db.db; //error
68+
~~
69+
!!! error TS2702: 'db' only refers to a type, but is being used as a namespace here.
70+
~~
71+
!!! error TS4031: Public property 'db' of exported class has or is using private name 'db'.
72+
73+
constructor(db: db.db) { // error
74+
~~
75+
!!! error TS2702: 'db' only refers to a type, but is being used as a namespace here.
76+
~~
77+
!!! error TS4063: Parameter 'db' of constructor from exported class has or is using private name 'db'.
78+
this.db = db;
79+
this.db.doSomething();
80+
}
81+
}
82+
export {MyClass};
83+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//// [tests/cases/compiler/typeReferenceDirectives13.ts] ////
2+
3+
//// [/app.ts]
4+
/// <reference types="lib"/>
5+
import {$} from "./ref";
6+
export interface A {
7+
x: () => typeof $
8+
}
9+
10+
//// [/ref.d.ts]
11+
export interface $ { x }
12+
13+
//// [/types/lib/index.d.ts]
14+
declare let $: { x: number }
15+
16+
17+
/// [Declarations] ////
18+
19+
20+
21+
//// [/app.d.ts]
22+
import { $ } from "./ref";
23+
export interface A {
24+
x: () => typeof $;
25+
}
26+
27+
/// [Errors] ////
28+
29+
/app.ts(1,23): error TS9010: Reference directives are not supported in isolated declaration mode.
30+
31+
32+
==== /app.ts (1 errors) ====
33+
/// <reference types="lib"/>
34+
~~~
35+
!!! error TS9010: Reference directives are not supported in isolated declaration mode.
36+
import {$} from "./ref";
37+
export interface A {
38+
x: () => typeof $
39+
}
40+
41+
==== /ref.d.ts (0 errors) ====
42+
export interface $ { x }
43+
44+
==== /types/lib/index.d.ts (0 errors) ====
45+
declare let $: { x: number }
46+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [tests/cases/compiler/typeReferenceDirectives5.ts] ////
2+
3+
//// [/app.ts]
4+
/// <reference types="lib"/>
5+
import {$} from "./ref";
6+
export interface A {
7+
x: typeof $;
8+
}
9+
//// [/ref.d.ts]
10+
export interface $ { x }
11+
12+
//// [/types/lib/index.d.ts]
13+
declare let $: { x: number }
14+
15+
16+
/// [Declarations] ////
17+
18+
19+
20+
//// [/app.d.ts]
21+
import { $ } from "./ref";
22+
export interface A {
23+
x: typeof $;
24+
}
25+
26+
/// [Errors] ////
27+
28+
/app.ts(1,23): error TS9010: Reference directives are not supported in isolated declaration mode.
29+
30+
31+
==== /app.ts (1 errors) ====
32+
/// <reference types="lib"/>
33+
~~~
34+
!!! error TS9010: Reference directives are not supported in isolated declaration mode.
35+
import {$} from "./ref";
36+
export interface A {
37+
x: typeof $;
38+
}
39+
==== /ref.d.ts (0 errors) ====
40+
export interface $ { x }
41+
42+
==== /types/lib/index.d.ts (0 errors) ====
43+
declare let $: { x: number }
44+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//// [tests/cases/compiler/decoratorMetadataWithImportDeclarationNameCollision7.ts] ////
2+
3+
//// [db.ts]
4+
export default class db {
5+
public doSomething() {
6+
}
7+
}
8+
9+
//// [service.ts]
10+
import db from './db';
11+
function someDecorator(target) {
12+
return target;
13+
}
14+
@someDecorator
15+
class MyClass {
16+
db: db.db; //error
17+
18+
constructor(db: db.db) { // error
19+
this.db = db;
20+
this.db.doSomething();
21+
}
22+
}
23+
export {MyClass};
24+
25+
26+
/// [Declarations] ////
27+
28+
29+
30+
//// [db.d.ts]
31+
export default class db {
32+
doSomething(): invalid;
33+
}
34+
35+
//// [service.d.ts]
36+
declare class MyClass {
37+
db: db.db;
38+
constructor(db: db.db);
39+
}
40+
export { MyClass };
41+
42+
/// [Errors] ////
43+
44+
db.ts(2,12): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit.
45+
service.ts(7,9): error TS2702: 'db' only refers to a type, but is being used as a namespace here.
46+
service.ts(7,9): error TS4031: Public property 'db' of exported class has or is using private name 'db'.
47+
service.ts(9,21): error TS2702: 'db' only refers to a type, but is being used as a namespace here.
48+
service.ts(9,21): error TS4063: Parameter 'db' of constructor from exported class has or is using private name 'db'.
49+
50+
51+
==== db.ts (1 errors) ====
52+
export default class db {
53+
public doSomething() {
54+
~~~~~~~~~~~
55+
!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit.
56+
}
57+
}
58+
59+
==== service.ts (4 errors) ====
60+
import db from './db';
61+
function someDecorator(target) {
62+
return target;
63+
}
64+
@someDecorator
65+
class MyClass {
66+
db: db.db; //error
67+
~~
68+
!!! error TS2702: 'db' only refers to a type, but is being used as a namespace here.
69+
~~
70+
!!! error TS4031: Public property 'db' of exported class has or is using private name 'db'.
71+
72+
constructor(db: db.db) { // error
73+
~~
74+
!!! error TS2702: 'db' only refers to a type, but is being used as a namespace here.
75+
~~
76+
!!! error TS4063: Parameter 'db' of constructor from exported class has or is using private name 'db'.
77+
this.db = db;
78+
this.db.doSomething();
79+
}
80+
}
81+
export {MyClass};
82+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//// [tests/cases/compiler/typeReferenceDirectives13.ts] ////
2+
3+
//// [/app.ts]
4+
/// <reference types="lib"/>
5+
import {$} from "./ref";
6+
export interface A {
7+
x: () => typeof $
8+
}
9+
10+
//// [/ref.d.ts]
11+
export interface $ { x }
12+
13+
//// [/types/lib/index.d.ts]
14+
declare let $: { x: number }
15+
16+
17+
/// [Declarations] ////
18+
19+
20+
21+
//// [/app.d.ts]
22+
export interface A {
23+
x: () => typeof $;
24+
}
25+
26+
/// [Errors] ////
27+
28+
/app.ts(1,23): error TS9010: Reference directives are not supported in isolated declaration mode.
29+
30+
31+
==== /app.ts (1 errors) ====
32+
/// <reference types="lib"/>
33+
~~~
34+
!!! error TS9010: Reference directives are not supported in isolated declaration mode.
35+
import {$} from "./ref";
36+
export interface A {
37+
x: () => typeof $
38+
}
39+
40+
==== /ref.d.ts (0 errors) ====
41+
export interface $ { x }
42+
43+
==== /types/lib/index.d.ts (0 errors) ====
44+
declare let $: { x: number }
45+

0 commit comments

Comments
 (0)