Fixed na issue with evaluator using binding element initializers as const values (#55910)

This commit is contained in:
Mateusz Burzyński 2023-09-29 22:23:46 +02:00 коммит произвёл GitHub
Родитель 485a2ea0c9
Коммит 38d09d4ae1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 236 добавлений и 2 удалений

Просмотреть файл

@ -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);
}
}

Просмотреть файл

@ -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'.
}

Просмотреть файл

@ -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 = {}));

Просмотреть файл

@ -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))
}

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -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))
}

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -0,0 +1,12 @@
// @strict: true
type Thing = {
value?: string | number;
};
declare const thing: Thing;
const { value = "123" } = thing;
enum E {
test = value,
}