Skip to content

Commit 924810c

Browse files
authored
Adds the symbol name to the error message for TS2742 (#63200)
1 parent 6cf8170 commit 924810c

24 files changed

+152
-39
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6435,7 +6435,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
64356435
if (name.includes("/node_modules/")) {
64366436
context.encounteredError = true;
64376437
if (context.tracker.reportLikelyUnsafeImportRequiredError) {
6438-
context.tracker.reportLikelyUnsafeImportRequiredError(name);
6438+
context.tracker.reportLikelyUnsafeImportRequiredError(name, nodeSymbol ? unescapeLeadingUnderscores(nodeSymbol.escapedName) : undefined);
64396439
}
64406440
}
64416441
if (name !== originalName) {
@@ -8090,8 +8090,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
80908090
reportInaccessibleUniqueSymbolError() {
80918091
markError(() => oldTracker.reportInaccessibleUniqueSymbolError());
80928092
},
8093-
reportLikelyUnsafeImportRequiredError(specifier) {
8094-
markError(() => oldTracker.reportLikelyUnsafeImportRequiredError(specifier));
8093+
reportLikelyUnsafeImportRequiredError(specifier, symbolName) {
8094+
markError(() => oldTracker.reportLikelyUnsafeImportRequiredError(specifier, symbolName));
80958095
},
80968096
reportNonSerializableProperty(name) {
80978097
markError(() => oldTracker.reportNonSerializableProperty(name));
@@ -8717,7 +8717,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
87178717
// since declaration files with these kinds of references are liable to fail when published :(
87188718
context.encounteredError = true;
87198719
if (context.tracker.reportLikelyUnsafeImportRequiredError) {
8720-
context.tracker.reportLikelyUnsafeImportRequiredError(oldSpecifier);
8720+
context.tracker.reportLikelyUnsafeImportRequiredError(oldSpecifier, unescapeLeadingUnderscores(symbol.escapedName));
87218721
}
87228722
}
87238723
}
@@ -54370,10 +54370,10 @@ class SymbolTrackerImpl implements SymbolTracker {
5437054370
}
5437154371
}
5437254372

54373-
reportLikelyUnsafeImportRequiredError(specifier: string): void {
54373+
reportLikelyUnsafeImportRequiredError(specifier: string, symbolName: string | undefined): void {
5437454374
if (this.inner?.reportLikelyUnsafeImportRequiredError) {
5437554375
this.onDiagnosticReported();
54376-
this.inner.reportLikelyUnsafeImportRequiredError(specifier);
54376+
this.inner.reportLikelyUnsafeImportRequiredError(specifier, symbolName);
5437754377
}
5437854378
}
5437954379

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4022,6 +4022,10 @@
40224022
"category": "Error",
40234023
"code": 2882
40244024
},
4025+
"The inferred type of '{0}' cannot be named without a reference to '{2}' from '{1}'. This is likely not portable. A type annotation is necessary.": {
4026+
"category": "Error",
4027+
"code": 2883
4028+
},
40254029

40264030
"Import declaration '{0}' is using private name '{1}'.": {
40274031
"category": "Error",

src/compiler/transformers/declarations.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,14 @@ export function transformDeclarations(context: TransformationContext): Transform
408408
}
409409
}
410410

411-
function reportLikelyUnsafeImportRequiredError(specifier: string) {
411+
function reportLikelyUnsafeImportRequiredError(specifier: string, symbolName: string | undefined) {
412412
if (errorNameNode || errorFallbackNode) {
413-
context.addDiagnostic(createDiagnosticForNode((errorNameNode || errorFallbackNode)!, Diagnostics.The_inferred_type_of_0_cannot_be_named_without_a_reference_to_1_This_is_likely_not_portable_A_type_annotation_is_necessary, errorDeclarationNameWithFallback(), specifier));
413+
if (symbolName) {
414+
context.addDiagnostic(createDiagnosticForNode((errorNameNode || errorFallbackNode)!, Diagnostics.The_inferred_type_of_0_cannot_be_named_without_a_reference_to_2_from_1_This_is_likely_not_portable_A_type_annotation_is_necessary, errorDeclarationNameWithFallback(), specifier, symbolName));
415+
}
416+
else {
417+
context.addDiagnostic(createDiagnosticForNode((errorNameNode || errorFallbackNode)!, Diagnostics.The_inferred_type_of_0_cannot_be_named_without_a_reference_to_1_This_is_likely_not_portable_A_type_annotation_is_necessary, errorDeclarationNameWithFallback(), specifier));
418+
}
414419
}
415420
}
416421

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10050,7 +10050,7 @@ export interface SymbolTracker {
1005010050
reportPrivateInBaseOfClassExpression?(propertyName: string): void;
1005110051
reportInaccessibleUniqueSymbolError?(): void;
1005210052
reportCyclicStructureError?(): void;
10053-
reportLikelyUnsafeImportRequiredError?(specifier: string): void;
10053+
reportLikelyUnsafeImportRequiredError?(specifier: string, symbolName: string | undefined): void;
1005410054
reportTruncationError?(): void;
1005510055
moduleResolverHost?: ModuleSpecifierResolutionHost & { getCommonSourceDirectory(): string; };
1005610056
reportNonlocalAugmentation?(containingFile: SourceFile, parentSymbol: Symbol, augmentingSymbol: Symbol): void;

tests/baselines/reference/declarationEmitCommonJsModuleReferencedType.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
r/entry.ts(3,14): error TS2742: The inferred type of 'x' cannot be named without a reference to 'foo/node_modules/nested'. This is likely not portable. A type annotation is necessary.
1+
r/entry.ts(3,14): error TS2883: The inferred type of 'x' cannot be named without a reference to 'NestedProps' from 'foo/node_modules/nested'. This is likely not portable. A type annotation is necessary.
22

33

44
==== r/node_modules/foo/node_modules/nested/index.d.ts (0 errors) ====
@@ -23,6 +23,6 @@ r/entry.ts(3,14): error TS2742: The inferred type of 'x' cannot be named without
2323
import { bar } from "root";
2424
export const x = foo();
2525
~
26-
!!! error TS2742: The inferred type of 'x' cannot be named without a reference to 'foo/node_modules/nested'. This is likely not portable. A type annotation is necessary.
26+
!!! error TS2883: The inferred type of 'x' cannot be named without a reference to 'NestedProps' from 'foo/node_modules/nested'. This is likely not portable. A type annotation is necessary.
2727
export const y = bar();
2828

tests/baselines/reference/declarationEmitObjectAssignedDefaultExport.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
index.ts(7,1): error TS2742: The inferred type of 'default' cannot be named without a reference to 'styled-components/node_modules/hoist-non-react-statics'. This is likely not portable. A type annotation is necessary.
1+
index.ts(7,1): error TS2883: The inferred type of 'default' cannot be named without a reference to 'NonReactStatics' from 'styled-components/node_modules/hoist-non-react-statics'. This is likely not portable. A type annotation is necessary.
22

33

44
==== node_modules/styled-components/node_modules/hoist-non-react-statics/index.d.ts (0 errors) ====
@@ -43,5 +43,5 @@ index.ts(7,1): error TS2742: The inferred type of 'default' cannot be named with
4343
~~~~~
4444
});
4545
~~~
46-
!!! error TS2742: The inferred type of 'default' cannot be named without a reference to 'styled-components/node_modules/hoist-non-react-statics'. This is likely not portable. A type annotation is necessary.
46+
!!! error TS2883: The inferred type of 'default' cannot be named without a reference to 'NonReactStatics' from 'styled-components/node_modules/hoist-non-react-statics'. This is likely not portable. A type annotation is necessary.
4747

tests/baselines/reference/declarationEmitReexportedSymlinkReference3.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
monorepo/pkg3/src/keys.ts(3,14): error TS2742: The inferred type of 'ADMIN' cannot be named without a reference to '../../pkg2/node_modules/@raymondfeng/pkg1/dist'. This is likely not portable. A type annotation is necessary.
1+
monorepo/pkg3/src/keys.ts(3,14): error TS2883: The inferred type of 'ADMIN' cannot be named without a reference to 'IdType' from '../../pkg2/node_modules/@raymondfeng/pkg1/dist'. This is likely not portable. A type annotation is necessary.
22

33

44
==== monorepo/pkg3/tsconfig.json (0 errors) ====
@@ -21,7 +21,7 @@ monorepo/pkg3/src/keys.ts(3,14): error TS2742: The inferred type of 'ADMIN' cann
2121

2222
export const ADMIN = MetadataAccessor.create<boolean>('1');
2323
~~~~~
24-
!!! error TS2742: The inferred type of 'ADMIN' cannot be named without a reference to '../../pkg2/node_modules/@raymondfeng/pkg1/dist'. This is likely not portable. A type annotation is necessary.
24+
!!! error TS2883: The inferred type of 'ADMIN' cannot be named without a reference to 'IdType' from '../../pkg2/node_modules/@raymondfeng/pkg1/dist'. This is likely not portable. A type annotation is necessary.
2525
==== monorepo/pkg1/dist/index.d.ts (0 errors) ====
2626
export * from './types';
2727
==== monorepo/pkg1/dist/types.d.ts (0 errors) ====
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
r/entry.ts(2,14): error TS2883: The inferred type of 'special' cannot be named without a reference to 'MySpecialType' from 'foo/node_modules/nested'. This is likely not portable. A type annotation is necessary.
2+
3+
4+
==== r/node_modules/foo/node_modules/nested/index.d.ts (0 errors) ====
5+
export interface MySpecialType {
6+
val: string;
7+
}
8+
==== r/node_modules/foo/index.d.ts (0 errors) ====
9+
import { MySpecialType } from "nested";
10+
export function getSpecial(): MySpecialType;
11+
==== r/entry.ts (1 errors) ====
12+
import { getSpecial } from "foo";
13+
export const special = getSpecial();
14+
~~~~~~~
15+
!!! error TS2883: The inferred type of 'special' cannot be named without a reference to 'MySpecialType' from 'foo/node_modules/nested'. This is likely not portable. A type annotation is necessary.
16+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [tests/cases/compiler/declarationEmitUnsafeImportSymbolName.ts] ////
2+
3+
//// [index.d.ts]
4+
export interface MySpecialType {
5+
val: string;
6+
}
7+
//// [index.d.ts]
8+
import { MySpecialType } from "nested";
9+
export function getSpecial(): MySpecialType;
10+
//// [entry.ts]
11+
import { getSpecial } from "foo";
12+
export const special = getSpecial();
13+
14+
15+
//// [entry.js]
16+
"use strict";
17+
Object.defineProperty(exports, "__esModule", { value: true });
18+
exports.special = void 0;
19+
const foo_1 = require("foo");
20+
exports.special = (0, foo_1.getSpecial)();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [tests/cases/compiler/declarationEmitUnsafeImportSymbolName.ts] ////
2+
3+
=== r/node_modules/foo/node_modules/nested/index.d.ts ===
4+
export interface MySpecialType {
5+
>MySpecialType : Symbol(MySpecialType, Decl(index.d.ts, 0, 0))
6+
7+
val: string;
8+
>val : Symbol(MySpecialType.val, Decl(index.d.ts, 0, 32))
9+
}
10+
=== r/node_modules/foo/index.d.ts ===
11+
import { MySpecialType } from "nested";
12+
>MySpecialType : Symbol(MySpecialType, Decl(index.d.ts, 0, 8))
13+
14+
export function getSpecial(): MySpecialType;
15+
>getSpecial : Symbol(getSpecial, Decl(index.d.ts, 0, 39))
16+
>MySpecialType : Symbol(MySpecialType, Decl(index.d.ts, 0, 8))
17+
18+
=== r/entry.ts ===
19+
import { getSpecial } from "foo";
20+
>getSpecial : Symbol(getSpecial, Decl(entry.ts, 0, 8))
21+
22+
export const special = getSpecial();
23+
>special : Symbol(special, Decl(entry.ts, 1, 12))
24+
>getSpecial : Symbol(getSpecial, Decl(entry.ts, 0, 8))
25+

0 commit comments

Comments
 (0)