mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-25 02:00:48 -05:00
Allow references to const variables with literal types in constant expressions
This commit is contained in:
@@ -46726,17 +46726,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
}
|
||||
if (isConstantVariable(symbol)) {
|
||||
const declaration = symbol.valueDeclaration;
|
||||
if (declaration && isVariableDeclaration(declaration) && !declaration.type && declaration.initializer && (!location || declaration !== location && isBlockScopedNameDeclaredBeforeUse(declaration, location))) {
|
||||
const result = evaluate(declaration.initializer, declaration);
|
||||
if (location && getSourceFileOfNode(location) !== getSourceFileOfNode(declaration)) {
|
||||
return evaluatorResult(
|
||||
result.value,
|
||||
/*isSyntacticallyString*/ false,
|
||||
/*resolvedOtherFiles*/ true,
|
||||
/*hasExternalReferences*/ true,
|
||||
);
|
||||
if (declaration && isVariableDeclaration(declaration)) {
|
||||
if (!declaration.type && declaration.initializer && (!location || declaration !== location && isBlockScopedNameDeclaredBeforeUse(declaration, location))) {
|
||||
const result = evaluate(declaration.initializer, declaration);
|
||||
return location && getSourceFileOfNode(location) !== getSourceFileOfNode(declaration) ?
|
||||
evaluatorResult(result.value, /*isSyntacticallyString*/ false, /*resolvedOtherFiles*/ true, /*hasExternalReferences*/ true) :
|
||||
evaluatorResult(result.value, result.isSyntacticallyString, result.resolvedOtherFiles, /*hasExternalReferences*/ true);
|
||||
}
|
||||
if (declaration.type && declaration.type.kind === SyntaxKind.LiteralType) {
|
||||
const type = getTypeOfSymbol(symbol);
|
||||
if (type.flags & (TypeFlags.StringLiteral | TypeFlags.NumberLiteral)) {
|
||||
return evaluatorResult((type as LiteralType).value as string | number);
|
||||
}
|
||||
}
|
||||
return evaluatorResult(result.value, result.isSyntacticallyString, result.resolvedOtherFiles, /*hasExternalReferences*/ true);
|
||||
}
|
||||
}
|
||||
return evaluatorResult(/*value*/ undefined);
|
||||
|
||||
Reference in New Issue
Block a user