@@ -33862,7 +33862,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
33862
33862
return objectType;
33863
33863
}
33864
33864
33865
- if (isConstEnumObjectType(objectType) && !isStringLiteralLike(indexExpression)) {
33865
+ // TODO(jakebailey): write test case
33866
+ if (isConstEnumObjectType(objectType) && !isStringLiteralLike(indexExpression) && !isPreservedConstEnum(objectType)) {
33866
33867
error(indexExpression, Diagnostics.A_const_enum_member_can_only_be_accessed_using_a_string_literal);
33867
33868
return errorType;
33868
33869
}
@@ -39793,26 +39794,29 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
39793
39794
(node.parent.kind === SyntaxKind.TypeQuery && (node.parent as TypeQueryNode).exprName === node)) ||
39794
39795
(node.parent.kind === SyntaxKind.ExportSpecifier); // We allow reexporting const enums
39795
39796
39796
- if (!ok) {
39797
- // TODO(jakebailey): dedupe
39798
- Debug.assert(!!(type.symbol.flags & SymbolFlags.ConstEnum));
39799
- const constEnumDeclaration = type.symbol.valueDeclaration as EnumDeclaration;
39800
- const otherFile = getSourceFileOfNode(constEnumDeclaration);
39801
- const redirect = host.getRedirectReferenceForResolutionFromSourceOfProject(otherFile.resolvedPath );
39802
- const isLegalValueAccess = !otherFile.isDeclarationFile || (redirect && shouldPreserveConstEnums(redirect.commandLine.options))
39803
- if (!isLegalValueAccess) {
39804
- error(node, Diagnostics.const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment_or_type_query);
39797
+ if (!ok || getIsolatedModules(compilerOptions) ) {
39798
+ if (!isPreservedConstEnum(type)) {
39799
+ if (!ok) {
39800
+ // TODO(jakebailey): write negative test case
39801
+ // TODO(jakebailey): make error message mention const enum preservation
39802
+ error(node, Diagnostics.const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment_or_type_query );
39803
+ } else if (type.symbol.valueDeclaration!.flags & NodeFlags.Ambient && !isValidTypeOnlyAliasUseSite(node)) {
39804
+ error(node, Diagnostics.Cannot_access_ambient_const_enums_when_0_is_enabled, isolatedModulesLikeFlagName);
39805
+ }
39805
39806
}
39806
39807
}
39808
+ }
39807
39809
39808
- if (getIsolatedModules(compilerOptions) ) {
39809
- Debug.assert(!!(type.symbol.flags & SymbolFlags.ConstEnum));
39810
- const constEnumDeclaration = type.symbol.valueDeclaration as EnumDeclaration;
39811
- const redirect = host.getRedirectReferenceForResolutionFromSourceOfProject( getSourceFileOfNode(constEnumDeclaration).resolvedPath );
39812
- if (constEnumDeclaration.flags & NodeFlags.Ambient && !isValidTypeOnlyAliasUseSite(node) && (!redirect || !shouldPreserveConstEnums(redirect.commandLine.options)) ) {
39813
- error(node, Diagnostics.Cannot_access_ambient_const_enums_when_0_is_enabled, isolatedModulesLikeFlagName);
39814
- }
39810
+ function isPreservedConstEnum(type: Type ) {
39811
+ Debug.assert(!!(type.symbol.flags & SymbolFlags.ConstEnum));
39812
+ const constEnumDeclaration = type.symbol.valueDeclaration as EnumDeclaration;
39813
+ const otherFile = getSourceFileOfNode(constEnumDeclaration);
39814
+ if (!otherFile.isDeclarationFile ) {
39815
+ // This file can only have come from the current project.
39816
+ return shouldPreserveConstEnums(compilerOptions);
39815
39817
}
39818
+ const redirect = host.getRedirectReferenceForResolutionFromSourceOfProject(otherFile.resolvedPath);
39819
+ return redirect && shouldPreserveConstEnums(redirect.commandLine.options)
39816
39820
}
39817
39821
39818
39822
function checkParenthesizedExpression(node: ParenthesizedExpression, checkMode?: CheckMode): Type {
0 commit comments