Skip to content

Commit 3acafe5

Browse files
authored
Consider property accesses in heritage clauses as type-space references for calculating type reference directives (#22746)
1 parent c861fa9 commit 3acafe5

5 files changed

+107
-4
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25982,18 +25982,23 @@ namespace ts {
2598225982
getJsxFactoryEntity: location => location ? (getJsxNamespace(location), (getSourceFileOfNode(location).localJsxFactory || _jsxFactoryEntity)) : _jsxFactoryEntity
2598325983
};
2598425984

25985+
function isInHeritageClause(node: PropertyAccessEntityNameExpression) {
25986+
return node.parent && node.parent.kind === SyntaxKind.ExpressionWithTypeArguments && node.parent.parent && node.parent.parent.kind === SyntaxKind.HeritageClause;
25987+
}
25988+
2598525989
// defined here to avoid outer scope pollution
2598625990
function getTypeReferenceDirectivesForEntityName(node: EntityNameOrEntityNameExpression): string[] {
2598725991
// program does not have any files with type reference directives - bail out
2598825992
if (!fileToDirective) {
2598925993
return undefined;
2599025994
}
25991-
// property access can only be used as values
25995+
// property access can only be used as values, or types when within an expression with type arguments inside a heritage clause
2599225996
// qualified names can only be used as types\namespaces
2599325997
// identifiers are treated as values only if they appear in type queries
25994-
const meaning = (node.kind === SyntaxKind.PropertyAccessExpression) || (node.kind === SyntaxKind.Identifier && isInTypeQuery(node))
25995-
? SymbolFlags.Value | SymbolFlags.ExportValue
25996-
: SymbolFlags.Type | SymbolFlags.Namespace;
25998+
let meaning = SymbolFlags.Type | SymbolFlags.Namespace;
25999+
if ((node.kind === SyntaxKind.Identifier && isInTypeQuery(node)) || (node.kind === SyntaxKind.PropertyAccessExpression && !isInHeritageClause(node))) {
26000+
meaning = SymbolFlags.Value | SymbolFlags.ExportValue;
26001+
}
2599726002

2599826003
const symbol = resolveEntityName(node, meaning, /*ignoreErrors*/ true);
2599926004
return symbol && symbol !== unknownSymbol ? getTypeReferenceDirectivesForSymbol(symbol, meaning) : undefined;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//// [tests/cases/compiler/declarationEmitHasTypesRefOnNamespaceUse.ts] ////
2+
3+
//// [dep.d.ts]
4+
declare namespace NS {
5+
interface Dep {
6+
}
7+
}
8+
//// [package.json]
9+
{
10+
"typings": "dep.d.ts"
11+
}
12+
//// [index.ts]
13+
class Src implements NS.Dep { }
14+
15+
16+
//// [index.js]
17+
var Src = /** @class */ (function () {
18+
function Src() {
19+
}
20+
return Src;
21+
}());
22+
23+
24+
//// [index.d.ts]
25+
/// <reference types="dep" />
26+
declare class Src implements NS.Dep {
27+
}
28+
29+
30+
//// [DtsFileErrors]
31+
32+
33+
error TS2688: Cannot find type definition file for 'dep'.
34+
/src/index.d.ts(1,23): error TS2688: Cannot find type definition file for 'dep'.
35+
/src/index.d.ts(2,30): error TS2503: Cannot find namespace 'NS'.
36+
37+
38+
!!! error TS2688: Cannot find type definition file for 'dep'.
39+
==== /src/index.d.ts (2 errors) ====
40+
/// <reference types="dep" />
41+
~~~
42+
!!! error TS2688: Cannot find type definition file for 'dep'.
43+
declare class Src implements NS.Dep {
44+
~~
45+
!!! error TS2503: Cannot find namespace 'NS'.
46+
}
47+
48+
==== /deps/dep/dep.d.ts (0 errors) ====
49+
declare namespace NS {
50+
interface Dep {
51+
}
52+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== /src/index.ts ===
2+
class Src implements NS.Dep { }
3+
>Src : Symbol(Src, Decl(index.ts, 0, 0))
4+
>NS.Dep : Symbol(NS.Dep, Decl(dep.d.ts, 0, 22))
5+
>NS : Symbol(NS, Decl(dep.d.ts, 0, 0))
6+
>Dep : Symbol(NS.Dep, Decl(dep.d.ts, 0, 22))
7+
8+
=== /deps/dep/dep.d.ts ===
9+
declare namespace NS {
10+
>NS : Symbol(NS, Decl(dep.d.ts, 0, 0))
11+
12+
interface Dep {
13+
>Dep : Symbol(Dep, Decl(dep.d.ts, 0, 22))
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== /src/index.ts ===
2+
class Src implements NS.Dep { }
3+
>Src : Src
4+
>NS.Dep : any
5+
>NS : any
6+
>Dep : NS.Dep
7+
8+
=== /deps/dep/dep.d.ts ===
9+
declare namespace NS {
10+
>NS : any
11+
12+
interface Dep {
13+
>Dep : Dep
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @declaration: true
2+
// @types: dep
3+
// @typeRoots: /deps
4+
// @currentDirectory: /
5+
// @noImplicitReferences: true
6+
// @filename: /deps/dep/dep.d.ts
7+
declare namespace NS {
8+
interface Dep {
9+
}
10+
}
11+
// @filename: /deps/dep/package.json
12+
{
13+
"typings": "dep.d.ts"
14+
}
15+
// @filename: /src/index.ts
16+
class Src implements NS.Dep { }

0 commit comments

Comments
 (0)