diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7f18bbc1a1dec..9d92c2bcbaf40 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -28732,7 +28732,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { // parameter declared in the same parameter list is a candidate. if (isIdentifier(expr)) { const symbol = getResolvedSymbol(expr); - const declaration = symbol.valueDeclaration; + const declaration = getExportSymbolOfValueSymbolIfExported(symbol).valueDeclaration; if (declaration && (isBindingElement(declaration) || isParameter(declaration)) && reference === declaration.parent && !declaration.initializer && !declaration.dotDotDotToken) { return declaration; } diff --git a/tests/baselines/reference/dependentDestructuredVariablesWithExport.symbols b/tests/baselines/reference/dependentDestructuredVariablesWithExport.symbols new file mode 100644 index 0000000000000..c65d71e28d6b4 --- /dev/null +++ b/tests/baselines/reference/dependentDestructuredVariablesWithExport.symbols @@ -0,0 +1,49 @@ +//// [tests/cases/conformance/controlFlow/dependentDestructuredVariablesWithExport.ts] //// + +=== dependentDestructuredVariablesWithExport.ts === +// https://github.com/microsoft/TypeScript/issues/59652 + +declare function mutuallyEnabledPair(): { +>mutuallyEnabledPair : Symbol(mutuallyEnabledPair, Decl(dependentDestructuredVariablesWithExport.ts, 0, 0)) + + discriminator: true, +>discriminator : Symbol(discriminator, Decl(dependentDestructuredVariablesWithExport.ts, 2, 41)) + + value: string, +>value : Symbol(value, Decl(dependentDestructuredVariablesWithExport.ts, 3, 24)) + + } | { + discriminator: false, +>discriminator : Symbol(discriminator, Decl(dependentDestructuredVariablesWithExport.ts, 5, 7)) + + value: null | undefined, +>value : Symbol(value, Decl(dependentDestructuredVariablesWithExport.ts, 6, 25)) + } + +const { discriminator: discriminator1, value: value1 } = mutuallyEnabledPair() +>discriminator : Symbol(discriminator, Decl(dependentDestructuredVariablesWithExport.ts, 2, 41), Decl(dependentDestructuredVariablesWithExport.ts, 5, 7)) +>discriminator1 : Symbol(discriminator1, Decl(dependentDestructuredVariablesWithExport.ts, 10, 7)) +>value : Symbol(value, Decl(dependentDestructuredVariablesWithExport.ts, 3, 24), Decl(dependentDestructuredVariablesWithExport.ts, 6, 25)) +>value1 : Symbol(value1, Decl(dependentDestructuredVariablesWithExport.ts, 10, 38)) +>mutuallyEnabledPair : Symbol(mutuallyEnabledPair, Decl(dependentDestructuredVariablesWithExport.ts, 0, 0)) + +if (discriminator1) { +>discriminator1 : Symbol(discriminator1, Decl(dependentDestructuredVariablesWithExport.ts, 10, 7)) + + value1; +>value1 : Symbol(value1, Decl(dependentDestructuredVariablesWithExport.ts, 10, 38)) +} + +export const { discriminator: discriminator2, value: value2 } = mutuallyEnabledPair() +>discriminator : Symbol(discriminator, Decl(dependentDestructuredVariablesWithExport.ts, 2, 41), Decl(dependentDestructuredVariablesWithExport.ts, 5, 7)) +>discriminator2 : Symbol(discriminator2, Decl(dependentDestructuredVariablesWithExport.ts, 16, 14)) +>value : Symbol(value, Decl(dependentDestructuredVariablesWithExport.ts, 3, 24), Decl(dependentDestructuredVariablesWithExport.ts, 6, 25)) +>value2 : Symbol(value2, Decl(dependentDestructuredVariablesWithExport.ts, 16, 45)) +>mutuallyEnabledPair : Symbol(mutuallyEnabledPair, Decl(dependentDestructuredVariablesWithExport.ts, 0, 0)) + +if (discriminator2) { +>discriminator2 : Symbol(discriminator2, Decl(dependentDestructuredVariablesWithExport.ts, 16, 14)) + + value2; +>value2 : Symbol(value2, Decl(dependentDestructuredVariablesWithExport.ts, 16, 45)) +} diff --git a/tests/baselines/reference/dependentDestructuredVariablesWithExport.types b/tests/baselines/reference/dependentDestructuredVariablesWithExport.types new file mode 100644 index 0000000000000..09386ce819b5e --- /dev/null +++ b/tests/baselines/reference/dependentDestructuredVariablesWithExport.types @@ -0,0 +1,76 @@ +//// [tests/cases/conformance/controlFlow/dependentDestructuredVariablesWithExport.ts] //// + +=== dependentDestructuredVariablesWithExport.ts === +// https://github.com/microsoft/TypeScript/issues/59652 + +declare function mutuallyEnabledPair(): { +>mutuallyEnabledPair : () => { discriminator: true; value: string; } | { discriminator: false; value: null | undefined; } +> : ^^^^^^ + + discriminator: true, +>discriminator : true +> : ^^^^ +>true : true +> : ^^^^ + + value: string, +>value : string +> : ^^^^^^ + + } | { + discriminator: false, +>discriminator : false +> : ^^^^^ +>false : false +> : ^^^^^ + + value: null | undefined, +>value : null | undefined +> : ^^^^^^^^^^^^^^^^ + } + +const { discriminator: discriminator1, value: value1 } = mutuallyEnabledPair() +>discriminator : any +> : ^^^ +>discriminator1 : boolean +> : ^^^^^^^ +>value : any +> : ^^^ +>value1 : string | null | undefined +> : ^^^^^^^^^^^^^^^^^^^^^^^^^ +>mutuallyEnabledPair() : { discriminator: true; value: string; } | { discriminator: false; value: null | undefined; } +> : ^^^^^^^^^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^ ^^^ +>mutuallyEnabledPair : () => { discriminator: true; value: string; } | { discriminator: false; value: null | undefined; } +> : ^^^^^^ + +if (discriminator1) { +>discriminator1 : boolean +> : ^^^^^^^ + + value1; +>value1 : string +> : ^^^^^^ +} + +export const { discriminator: discriminator2, value: value2 } = mutuallyEnabledPair() +>discriminator : any +> : ^^^ +>discriminator2 : boolean +> : ^^^^^^^ +>value : any +> : ^^^ +>value2 : string | null | undefined +> : ^^^^^^^^^^^^^^^^^^^^^^^^^ +>mutuallyEnabledPair() : { discriminator: true; value: string; } | { discriminator: false; value: null | undefined; } +> : ^^^^^^^^^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^ ^^^ +>mutuallyEnabledPair : () => { discriminator: true; value: string; } | { discriminator: false; value: null | undefined; } +> : ^^^^^^ + +if (discriminator2) { +>discriminator2 : boolean +> : ^^^^^^^ + + value2; +>value2 : string +> : ^^^^^^ +} diff --git a/tests/cases/conformance/controlFlow/dependentDestructuredVariablesWithExport.ts b/tests/cases/conformance/controlFlow/dependentDestructuredVariablesWithExport.ts new file mode 100644 index 0000000000000..69a7aab62d5a7 --- /dev/null +++ b/tests/cases/conformance/controlFlow/dependentDestructuredVariablesWithExport.ts @@ -0,0 +1,24 @@ +// @strict: true +// @noEmit: true + +// https://github.com/microsoft/TypeScript/issues/59652 + +declare function mutuallyEnabledPair(): { + discriminator: true, + value: string, + } | { + discriminator: false, + value: null | undefined, + } + +const { discriminator: discriminator1, value: value1 } = mutuallyEnabledPair() + +if (discriminator1) { + value1; +} + +export const { discriminator: discriminator2, value: value2 } = mutuallyEnabledPair() + +if (discriminator2) { + value2; +} \ No newline at end of file