diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d63d9760172..14c814fdb17 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -44780,8 +44780,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return location ? evaluateEnumMember(expr, symbol, location) : getEnumMemberValue(symbol.valueDeclaration as EnumMember); } if (isConstantVariable(symbol)) { - const declaration = symbol.valueDeclaration as VariableDeclaration | undefined; - if (declaration && !declaration.type && declaration.initializer && (!location || declaration !== location && isBlockScopedNameDeclaredBeforeUse(declaration, location))) { + const declaration = symbol.valueDeclaration; + if (declaration && isVariableDeclaration(declaration) && !declaration.type && declaration.initializer && (!location || declaration !== location && isBlockScopedNameDeclaredBeforeUse(declaration, location))) { return evaluate(declaration.initializer, declaration); } } diff --git a/tests/baselines/reference/enumErrorOnConstantBindingWithInitializer.errors.txt b/tests/baselines/reference/enumErrorOnConstantBindingWithInitializer.errors.txt new file mode 100644 index 00000000000..c455b62bd76 --- /dev/null +++ b/tests/baselines/reference/enumErrorOnConstantBindingWithInitializer.errors.txt @@ -0,0 +1,19 @@ +enumErrorOnConstantBindingWithInitializer.ts(9,10): error TS18033: Type 'string | number' is not assignable to type 'number' as required for computed enum member values. + Type 'string' is not assignable to type 'number'. + + +==== enumErrorOnConstantBindingWithInitializer.ts (1 errors) ==== + type Thing = { + value?: string | number; + }; + + declare const thing: Thing; + const { value = "123" } = thing; + + enum E { + test = value, + ~~~~~ +!!! error TS18033: Type 'string | number' is not assignable to type 'number' as required for computed enum member values. +!!! error TS18033: Type 'string' is not assignable to type 'number'. + } + \ No newline at end of file diff --git a/tests/baselines/reference/enumErrorOnConstantBindingWithInitializer.js b/tests/baselines/reference/enumErrorOnConstantBindingWithInitializer.js new file mode 100644 index 00000000000..3c42a0b49b4 --- /dev/null +++ b/tests/baselines/reference/enumErrorOnConstantBindingWithInitializer.js @@ -0,0 +1,22 @@ +//// [tests/cases/conformance/enums/enumErrorOnConstantBindingWithInitializer.ts] //// + +//// [enumErrorOnConstantBindingWithInitializer.ts] +type Thing = { + value?: string | number; +}; + +declare const thing: Thing; +const { value = "123" } = thing; + +enum E { + test = value, +} + + +//// [enumErrorOnConstantBindingWithInitializer.js] +"use strict"; +var _a = thing.value, value = _a === void 0 ? "123" : _a; +var E; +(function (E) { + E[E["test"] = value] = "test"; +})(E || (E = {})); diff --git a/tests/baselines/reference/enumErrorOnConstantBindingWithInitializer.symbols b/tests/baselines/reference/enumErrorOnConstantBindingWithInitializer.symbols new file mode 100644 index 00000000000..e242e89b7c3 --- /dev/null +++ b/tests/baselines/reference/enumErrorOnConstantBindingWithInitializer.symbols @@ -0,0 +1,27 @@ +//// [tests/cases/conformance/enums/enumErrorOnConstantBindingWithInitializer.ts] //// + +=== enumErrorOnConstantBindingWithInitializer.ts === +type Thing = { +>Thing : Symbol(Thing, Decl(enumErrorOnConstantBindingWithInitializer.ts, 0, 0)) + + value?: string | number; +>value : Symbol(value, Decl(enumErrorOnConstantBindingWithInitializer.ts, 0, 14)) + +}; + +declare const thing: Thing; +>thing : Symbol(thing, Decl(enumErrorOnConstantBindingWithInitializer.ts, 4, 13)) +>Thing : Symbol(Thing, Decl(enumErrorOnConstantBindingWithInitializer.ts, 0, 0)) + +const { value = "123" } = thing; +>value : Symbol(value, Decl(enumErrorOnConstantBindingWithInitializer.ts, 5, 7)) +>thing : Symbol(thing, Decl(enumErrorOnConstantBindingWithInitializer.ts, 4, 13)) + +enum E { +>E : Symbol(E, Decl(enumErrorOnConstantBindingWithInitializer.ts, 5, 32)) + + test = value, +>test : Symbol(E.test, Decl(enumErrorOnConstantBindingWithInitializer.ts, 7, 8)) +>value : Symbol(value, Decl(enumErrorOnConstantBindingWithInitializer.ts, 5, 7)) +} + diff --git a/tests/baselines/reference/enumErrorOnConstantBindingWithInitializer.types b/tests/baselines/reference/enumErrorOnConstantBindingWithInitializer.types new file mode 100644 index 00000000000..4318349a8eb --- /dev/null +++ b/tests/baselines/reference/enumErrorOnConstantBindingWithInitializer.types @@ -0,0 +1,27 @@ +//// [tests/cases/conformance/enums/enumErrorOnConstantBindingWithInitializer.ts] //// + +=== enumErrorOnConstantBindingWithInitializer.ts === +type Thing = { +>Thing : { value?: string | number | undefined; } + + value?: string | number; +>value : string | number | undefined + +}; + +declare const thing: Thing; +>thing : Thing + +const { value = "123" } = thing; +>value : string | number +>"123" : "123" +>thing : Thing + +enum E { +>E : E + + test = value, +>test : E.test +>value : string | number +} + diff --git a/tests/baselines/reference/templateExpressionNoInlininingOfConstantBindingWithInitializer.js b/tests/baselines/reference/templateExpressionNoInlininingOfConstantBindingWithInitializer.js new file mode 100644 index 00000000000..3f4f19ecb9a --- /dev/null +++ b/tests/baselines/reference/templateExpressionNoInlininingOfConstantBindingWithInitializer.js @@ -0,0 +1,29 @@ +//// [tests/cases/compiler/templateExpressionNoInlininingOfConstantBindingWithInitializer.ts] //// + +//// [templateExpressionNoInlininingOfConstantBindingWithInitializer.ts] +type Params = { + value?: string | number +} + +function example(parameters: Params) { + const { value = '123' } = parameters + return `${value}` === '345' +} + +function example2(parameters: Params) { + const { value = '123' } = parameters + const b = `${value}`; + return b; +} + + +//// [templateExpressionNoInlininingOfConstantBindingWithInitializer.js] +function example(parameters) { + var _a = parameters.value, value = _a === void 0 ? '123' : _a; + return "".concat(value) === '345'; +} +function example2(parameters) { + var _a = parameters.value, value = _a === void 0 ? '123' : _a; + var b = "".concat(value); + return b; +} diff --git a/tests/baselines/reference/templateExpressionNoInlininingOfConstantBindingWithInitializer.symbols b/tests/baselines/reference/templateExpressionNoInlininingOfConstantBindingWithInitializer.symbols new file mode 100644 index 00000000000..6c7677184e4 --- /dev/null +++ b/tests/baselines/reference/templateExpressionNoInlininingOfConstantBindingWithInitializer.symbols @@ -0,0 +1,40 @@ +//// [tests/cases/compiler/templateExpressionNoInlininingOfConstantBindingWithInitializer.ts] //// + +=== templateExpressionNoInlininingOfConstantBindingWithInitializer.ts === +type Params = { +>Params : Symbol(Params, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 0, 0)) + + value?: string | number +>value : Symbol(value, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 0, 15)) +} + +function example(parameters: Params) { +>example : Symbol(example, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 2, 1)) +>parameters : Symbol(parameters, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 4, 17)) +>Params : Symbol(Params, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 0, 0)) + + const { value = '123' } = parameters +>value : Symbol(value, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 5, 9)) +>parameters : Symbol(parameters, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 4, 17)) + + return `${value}` === '345' +>value : Symbol(value, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 5, 9)) +} + +function example2(parameters: Params) { +>example2 : Symbol(example2, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 7, 1)) +>parameters : Symbol(parameters, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 9, 18)) +>Params : Symbol(Params, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 0, 0)) + + const { value = '123' } = parameters +>value : Symbol(value, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 10, 9)) +>parameters : Symbol(parameters, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 9, 18)) + + const b = `${value}`; +>b : Symbol(b, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 11, 7)) +>value : Symbol(value, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 10, 9)) + + return b; +>b : Symbol(b, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 11, 7)) +} + diff --git a/tests/baselines/reference/templateExpressionNoInlininingOfConstantBindingWithInitializer.types b/tests/baselines/reference/templateExpressionNoInlininingOfConstantBindingWithInitializer.types new file mode 100644 index 00000000000..61329859278 --- /dev/null +++ b/tests/baselines/reference/templateExpressionNoInlininingOfConstantBindingWithInitializer.types @@ -0,0 +1,44 @@ +//// [tests/cases/compiler/templateExpressionNoInlininingOfConstantBindingWithInitializer.ts] //// + +=== templateExpressionNoInlininingOfConstantBindingWithInitializer.ts === +type Params = { +>Params : { value?: string | number; } + + value?: string | number +>value : string | number +} + +function example(parameters: Params) { +>example : (parameters: Params) => boolean +>parameters : Params + + const { value = '123' } = parameters +>value : string | number +>'123' : "123" +>parameters : Params + + return `${value}` === '345' +>`${value}` === '345' : boolean +>`${value}` : string +>value : string | number +>'345' : "345" +} + +function example2(parameters: Params) { +>example2 : (parameters: Params) => string +>parameters : Params + + const { value = '123' } = parameters +>value : string | number +>'123' : "123" +>parameters : Params + + const b = `${value}`; +>b : string +>`${value}` : string +>value : string | number + + return b; +>b : string +} + diff --git a/tests/cases/compiler/templateExpressionNoInlininingOfConstantBindingWithInitializer.ts b/tests/cases/compiler/templateExpressionNoInlininingOfConstantBindingWithInitializer.ts new file mode 100644 index 00000000000..70418ee4a92 --- /dev/null +++ b/tests/cases/compiler/templateExpressionNoInlininingOfConstantBindingWithInitializer.ts @@ -0,0 +1,14 @@ +type Params = { + value?: string | number +} + +function example(parameters: Params) { + const { value = '123' } = parameters + return `${value}` === '345' +} + +function example2(parameters: Params) { + const { value = '123' } = parameters + const b = `${value}`; + return b; +} diff --git a/tests/cases/conformance/enums/enumErrorOnConstantBindingWithInitializer.ts b/tests/cases/conformance/enums/enumErrorOnConstantBindingWithInitializer.ts new file mode 100644 index 00000000000..d4ccccdb7c2 --- /dev/null +++ b/tests/cases/conformance/enums/enumErrorOnConstantBindingWithInitializer.ts @@ -0,0 +1,12 @@ +// @strict: true + +type Thing = { + value?: string | number; +}; + +declare const thing: Thing; +const { value = "123" } = thing; + +enum E { + test = value, +}