Allow usage of local value symbol merged with type-only import (#47642)

This commit is contained in:
Andrew Branch
2022-01-27 16:18:14 -08:00
committed by GitHub
parent 90280518ae
commit 3de032fd0d
5 changed files with 73 additions and 18 deletions

View File

@@ -2235,29 +2235,23 @@ namespace ts {
error(errorLocation, Diagnostics.Parameter_0_cannot_reference_identifier_1_declared_after_it, declarationNameToString(associatedDeclarationForContainingInitializerOrBindingName.name), declarationNameToString(errorLocation as Identifier));
}
}
if (result && errorLocation && meaning & SymbolFlags.Value && result.flags & SymbolFlags.Alias) {
checkSymbolUsageInExpressionContext(result, name, errorLocation);
if (result && errorLocation && meaning & SymbolFlags.Value && result.flags & SymbolFlags.Alias && !(result.flags & SymbolFlags.Value) && !isValidTypeOnlyAliasUseSite(errorLocation)) {
const typeOnlyDeclaration = getTypeOnlyAliasDeclaration(result);
if (typeOnlyDeclaration) {
const message = typeOnlyDeclaration.kind === SyntaxKind.ExportSpecifier
? Diagnostics._0_cannot_be_used_as_a_value_because_it_was_exported_using_export_type
: Diagnostics._0_cannot_be_used_as_a_value_because_it_was_imported_using_import_type;
const unescapedName = unescapeLeadingUnderscores(name);
addTypeOnlyDeclarationRelatedInfo(
error(errorLocation, message, unescapedName),
typeOnlyDeclaration,
unescapedName);
}
}
}
return result;
}
function checkSymbolUsageInExpressionContext(symbol: Symbol, name: __String, useSite: Node) {
if (!isValidTypeOnlyAliasUseSite(useSite)) {
const typeOnlyDeclaration = getTypeOnlyAliasDeclaration(symbol);
if (typeOnlyDeclaration) {
const message = typeOnlyDeclaration.kind === SyntaxKind.ExportSpecifier
? Diagnostics._0_cannot_be_used_as_a_value_because_it_was_exported_using_export_type
: Diagnostics._0_cannot_be_used_as_a_value_because_it_was_imported_using_import_type;
const unescapedName = unescapeLeadingUnderscores(name);
addTypeOnlyDeclarationRelatedInfo(
error(useSite, message, unescapedName),
typeOnlyDeclaration,
unescapedName);
}
}
}
function addTypeOnlyDeclarationRelatedInfo(diagnostic: Diagnostic, typeOnlyDeclaration: TypeOnlyCompatibleAliasDeclaration | undefined, unescapedName: string) {
if (!typeOnlyDeclaration) return diagnostic;
return addRelatedInfo(

View File

@@ -0,0 +1,19 @@
//// [tests/cases/conformance/externalModules/typeOnly/mergedWithLocalValue.ts] ////
//// [a.ts]
export type A = "a";
//// [b.ts]
import type { A } from "./a";
const A: A = "a";
A.toUpperCase();
//// [a.js]
"use strict";
exports.__esModule = true;
//// [b.js]
"use strict";
exports.__esModule = true;
var A = "a";
A.toUpperCase();

View File

@@ -0,0 +1,17 @@
=== tests/cases/conformance/externalModules/typeOnly/a.ts ===
export type A = "a";
>A : Symbol(A, Decl(a.ts, 0, 0))
=== tests/cases/conformance/externalModules/typeOnly/b.ts ===
import type { A } from "./a";
>A : Symbol(A, Decl(b.ts, 0, 13), Decl(b.ts, 1, 5))
const A: A = "a";
>A : Symbol(A, Decl(b.ts, 0, 13), Decl(b.ts, 1, 5))
>A : Symbol(A, Decl(b.ts, 0, 13), Decl(b.ts, 1, 5))
A.toUpperCase();
>A.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
>A : Symbol(A, Decl(b.ts, 0, 13), Decl(b.ts, 1, 5))
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))

View File

@@ -0,0 +1,18 @@
=== tests/cases/conformance/externalModules/typeOnly/a.ts ===
export type A = "a";
>A : "a"
=== tests/cases/conformance/externalModules/typeOnly/b.ts ===
import type { A } from "./a";
>A : "a"
const A: A = "a";
>A : "a"
>"a" : "a"
A.toUpperCase();
>A.toUpperCase() : string
>A.toUpperCase : () => string
>A : "a"
>toUpperCase : () => string

View File

@@ -0,0 +1,7 @@
// @Filename: a.ts
export type A = "a";
// @Filename: b.ts
import type { A } from "./a";
const A: A = "a";
A.toUpperCase();