mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-10 10:58:20 -05:00
Allow usage of local value symbol merged with type-only import (#47642)
This commit is contained in:
@@ -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(
|
||||
|
||||
19
tests/baselines/reference/mergedWithLocalValue.js
Normal file
19
tests/baselines/reference/mergedWithLocalValue.js
Normal 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();
|
||||
17
tests/baselines/reference/mergedWithLocalValue.symbols
Normal file
17
tests/baselines/reference/mergedWithLocalValue.symbols
Normal 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, --, --))
|
||||
|
||||
18
tests/baselines/reference/mergedWithLocalValue.types
Normal file
18
tests/baselines/reference/mergedWithLocalValue.types
Normal 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
|
||||
|
||||
@@ -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();
|
||||
Reference in New Issue
Block a user