Skip to content

Commit 38d09d4

Browse files
authored
Fixed na issue with evaluator using binding element initializers as const values (#55910)
1 parent 485a2ea commit 38d09d4

10 files changed

+236
-2
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44780,8 +44780,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4478044780
return location ? evaluateEnumMember(expr, symbol, location) : getEnumMemberValue(symbol.valueDeclaration as EnumMember);
4478144781
}
4478244782
if (isConstantVariable(symbol)) {
44783-
const declaration = symbol.valueDeclaration as VariableDeclaration | undefined;
44784-
if (declaration && !declaration.type && declaration.initializer && (!location || declaration !== location && isBlockScopedNameDeclaredBeforeUse(declaration, location))) {
44783+
const declaration = symbol.valueDeclaration;
44784+
if (declaration && isVariableDeclaration(declaration) && !declaration.type && declaration.initializer && (!location || declaration !== location && isBlockScopedNameDeclaredBeforeUse(declaration, location))) {
4478544785
return evaluate(declaration.initializer, declaration);
4478644786
}
4478744787
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
enumErrorOnConstantBindingWithInitializer.ts(9,10): error TS18033: Type 'string | number' is not assignable to type 'number' as required for computed enum member values.
2+
Type 'string' is not assignable to type 'number'.
3+
4+
5+
==== enumErrorOnConstantBindingWithInitializer.ts (1 errors) ====
6+
type Thing = {
7+
value?: string | number;
8+
};
9+
10+
declare const thing: Thing;
11+
const { value = "123" } = thing;
12+
13+
enum E {
14+
test = value,
15+
~~~~~
16+
!!! error TS18033: Type 'string | number' is not assignable to type 'number' as required for computed enum member values.
17+
!!! error TS18033: Type 'string' is not assignable to type 'number'.
18+
}
19+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/conformance/enums/enumErrorOnConstantBindingWithInitializer.ts] ////
2+
3+
//// [enumErrorOnConstantBindingWithInitializer.ts]
4+
type Thing = {
5+
value?: string | number;
6+
};
7+
8+
declare const thing: Thing;
9+
const { value = "123" } = thing;
10+
11+
enum E {
12+
test = value,
13+
}
14+
15+
16+
//// [enumErrorOnConstantBindingWithInitializer.js]
17+
"use strict";
18+
var _a = thing.value, value = _a === void 0 ? "123" : _a;
19+
var E;
20+
(function (E) {
21+
E[E["test"] = value] = "test";
22+
})(E || (E = {}));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/conformance/enums/enumErrorOnConstantBindingWithInitializer.ts] ////
2+
3+
=== enumErrorOnConstantBindingWithInitializer.ts ===
4+
type Thing = {
5+
>Thing : Symbol(Thing, Decl(enumErrorOnConstantBindingWithInitializer.ts, 0, 0))
6+
7+
value?: string | number;
8+
>value : Symbol(value, Decl(enumErrorOnConstantBindingWithInitializer.ts, 0, 14))
9+
10+
};
11+
12+
declare const thing: Thing;
13+
>thing : Symbol(thing, Decl(enumErrorOnConstantBindingWithInitializer.ts, 4, 13))
14+
>Thing : Symbol(Thing, Decl(enumErrorOnConstantBindingWithInitializer.ts, 0, 0))
15+
16+
const { value = "123" } = thing;
17+
>value : Symbol(value, Decl(enumErrorOnConstantBindingWithInitializer.ts, 5, 7))
18+
>thing : Symbol(thing, Decl(enumErrorOnConstantBindingWithInitializer.ts, 4, 13))
19+
20+
enum E {
21+
>E : Symbol(E, Decl(enumErrorOnConstantBindingWithInitializer.ts, 5, 32))
22+
23+
test = value,
24+
>test : Symbol(E.test, Decl(enumErrorOnConstantBindingWithInitializer.ts, 7, 8))
25+
>value : Symbol(value, Decl(enumErrorOnConstantBindingWithInitializer.ts, 5, 7))
26+
}
27+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/conformance/enums/enumErrorOnConstantBindingWithInitializer.ts] ////
2+
3+
=== enumErrorOnConstantBindingWithInitializer.ts ===
4+
type Thing = {
5+
>Thing : { value?: string | number | undefined; }
6+
7+
value?: string | number;
8+
>value : string | number | undefined
9+
10+
};
11+
12+
declare const thing: Thing;
13+
>thing : Thing
14+
15+
const { value = "123" } = thing;
16+
>value : string | number
17+
>"123" : "123"
18+
>thing : Thing
19+
20+
enum E {
21+
>E : E
22+
23+
test = value,
24+
>test : E.test
25+
>value : string | number
26+
}
27+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [tests/cases/compiler/templateExpressionNoInlininingOfConstantBindingWithInitializer.ts] ////
2+
3+
//// [templateExpressionNoInlininingOfConstantBindingWithInitializer.ts]
4+
type Params = {
5+
value?: string | number
6+
}
7+
8+
function example(parameters: Params) {
9+
const { value = '123' } = parameters
10+
return `${value}` === '345'
11+
}
12+
13+
function example2(parameters: Params) {
14+
const { value = '123' } = parameters
15+
const b = `${value}`;
16+
return b;
17+
}
18+
19+
20+
//// [templateExpressionNoInlininingOfConstantBindingWithInitializer.js]
21+
function example(parameters) {
22+
var _a = parameters.value, value = _a === void 0 ? '123' : _a;
23+
return "".concat(value) === '345';
24+
}
25+
function example2(parameters) {
26+
var _a = parameters.value, value = _a === void 0 ? '123' : _a;
27+
var b = "".concat(value);
28+
return b;
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [tests/cases/compiler/templateExpressionNoInlininingOfConstantBindingWithInitializer.ts] ////
2+
3+
=== templateExpressionNoInlininingOfConstantBindingWithInitializer.ts ===
4+
type Params = {
5+
>Params : Symbol(Params, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 0, 0))
6+
7+
value?: string | number
8+
>value : Symbol(value, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 0, 15))
9+
}
10+
11+
function example(parameters: Params) {
12+
>example : Symbol(example, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 2, 1))
13+
>parameters : Symbol(parameters, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 4, 17))
14+
>Params : Symbol(Params, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 0, 0))
15+
16+
const { value = '123' } = parameters
17+
>value : Symbol(value, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 5, 9))
18+
>parameters : Symbol(parameters, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 4, 17))
19+
20+
return `${value}` === '345'
21+
>value : Symbol(value, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 5, 9))
22+
}
23+
24+
function example2(parameters: Params) {
25+
>example2 : Symbol(example2, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 7, 1))
26+
>parameters : Symbol(parameters, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 9, 18))
27+
>Params : Symbol(Params, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 0, 0))
28+
29+
const { value = '123' } = parameters
30+
>value : Symbol(value, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 10, 9))
31+
>parameters : Symbol(parameters, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 9, 18))
32+
33+
const b = `${value}`;
34+
>b : Symbol(b, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 11, 7))
35+
>value : Symbol(value, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 10, 9))
36+
37+
return b;
38+
>b : Symbol(b, Decl(templateExpressionNoInlininingOfConstantBindingWithInitializer.ts, 11, 7))
39+
}
40+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [tests/cases/compiler/templateExpressionNoInlininingOfConstantBindingWithInitializer.ts] ////
2+
3+
=== templateExpressionNoInlininingOfConstantBindingWithInitializer.ts ===
4+
type Params = {
5+
>Params : { value?: string | number; }
6+
7+
value?: string | number
8+
>value : string | number
9+
}
10+
11+
function example(parameters: Params) {
12+
>example : (parameters: Params) => boolean
13+
>parameters : Params
14+
15+
const { value = '123' } = parameters
16+
>value : string | number
17+
>'123' : "123"
18+
>parameters : Params
19+
20+
return `${value}` === '345'
21+
>`${value}` === '345' : boolean
22+
>`${value}` : string
23+
>value : string | number
24+
>'345' : "345"
25+
}
26+
27+
function example2(parameters: Params) {
28+
>example2 : (parameters: Params) => string
29+
>parameters : Params
30+
31+
const { value = '123' } = parameters
32+
>value : string | number
33+
>'123' : "123"
34+
>parameters : Params
35+
36+
const b = `${value}`;
37+
>b : string
38+
>`${value}` : string
39+
>value : string | number
40+
41+
return b;
42+
>b : string
43+
}
44+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type Params = {
2+
value?: string | number
3+
}
4+
5+
function example(parameters: Params) {
6+
const { value = '123' } = parameters
7+
return `${value}` === '345'
8+
}
9+
10+
function example2(parameters: Params) {
11+
const { value = '123' } = parameters
12+
const b = `${value}`;
13+
return b;
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @strict: true
2+
3+
type Thing = {
4+
value?: string | number;
5+
};
6+
7+
declare const thing: Thing;
8+
const { value = "123" } = thing;
9+
10+
enum E {
11+
test = value,
12+
}

0 commit comments

Comments
 (0)