@@ -33885,7 +33885,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3388533885 }
3388633886
3388733887 // TODO(jakebailey): write test case
33888- if (isConstEnumObjectType(objectType) && !isStringLiteralLike(indexExpression) && !isPreservedConstEnum( objectType)) {
33888+ if (isConstEnumObjectType(objectType) && !isStringLiteralLike(indexExpression) && !isPreservedConstEnumUse(node, objectType)) {
3388933889 error(indexExpression, Diagnostics.A_const_enum_member_can_only_be_accessed_using_a_string_literal);
3389033890 return errorType;
3389133891 }
@@ -39817,7 +39817,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3981739817 (node.parent.kind === SyntaxKind.ExportSpecifier); // We allow reexporting const enums
3981839818
3981939819 if (!ok || getIsolatedModules(compilerOptions)) {
39820- if (!isPreservedConstEnum( type)) {
39820+ if (!isPreservedConstEnumUse(node, type)) {
3982139821 if (!ok) {
3982239822 // TODO(jakebailey): write negative test case
3982339823 // TODO(jakebailey): make error message mention const enum preservation
@@ -39829,12 +39829,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3982939829 }
3983039830 }
3983139831
39832- function isPreservedConstEnum(type : Type) {
39833- Debug.assert(!!(type .symbol.flags & SymbolFlags.ConstEnum));
39834- const constEnumDeclaration = type .symbol.valueDeclaration as EnumDeclaration;
39832+ function isPreservedConstEnumUse(use: Node, enumType : Type) {
39833+ Debug.assert(!!(enumType .symbol.flags & SymbolFlags.ConstEnum));
39834+ const constEnumDeclaration = enumType .symbol.valueDeclaration as EnumDeclaration;
3983539835 const otherFile = getSourceFileOfNode(constEnumDeclaration);
3983639836 if (!otherFile.isDeclarationFile) {
3983739837 // This file can only have come from the current project.
39838+ if (constEnumDeclaration.flags & NodeFlags.Ambient && !isValidTypeOnlyAliasUseSite(use)) {
39839+ return false
39840+ }
3983839841 return shouldPreserveConstEnums(compilerOptions);
3983939842 }
3984039843 const redirect = host.getRedirectReferenceForResolutionFromSourceOfProject(otherFile.resolvedPath);
0 commit comments