Skip to content

Commit b44ac69

Browse files
authored
Keep returned (and yielded) literal types as const when their types using const type variables (#56859)
1 parent f6f4eab commit b44ac69

File tree

4 files changed

+1290
-2
lines changed

4 files changed

+1290
-2
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39094,6 +39094,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3909439094
let fallbackReturnType: Type = voidType;
3909539095
if (func.body.kind !== SyntaxKind.Block) { // Async or normal arrow function
3909639096
returnType = checkExpressionCached(func.body, checkMode && checkMode & ~CheckMode.SkipGenericFunctions);
39097+
if (isConstContext(func.body)) {
39098+
returnType = getRegularTypeOfLiteralType(returnType);
39099+
}
3909739100
if (isAsync) {
3909839101
// From within an async function you can return either a non-promise value or a promise. Any
3909939102
// Promise/A+ compatible implementation will always assimilate any foreign promise, so the
@@ -39205,7 +39208,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3920539208
const nextTypes: Type[] = [];
3920639209
const isAsync = (getFunctionFlags(func) & FunctionFlags.Async) !== 0;
3920739210
forEachYieldExpression(func.body as Block, yieldExpression => {
39208-
const yieldExpressionType = yieldExpression.expression ? checkExpression(yieldExpression.expression, checkMode) : undefinedWideningType;
39211+
let yieldExpressionType = yieldExpression.expression ? checkExpression(yieldExpression.expression, checkMode) : undefinedWideningType;
39212+
if (yieldExpression.expression && isConstContext(yieldExpression.expression)) {
39213+
yieldExpressionType = getRegularTypeOfLiteralType(yieldExpressionType);
39214+
}
3920939215
pushIfUnique(yieldTypes, getYieldedTypeOfYieldExpression(yieldExpression, yieldExpressionType, anyType, isAsync));
3921039216
let nextType: Type | undefined;
3921139217
if (yieldExpression.asteriskToken) {
@@ -39332,7 +39338,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3933239338
if (type.flags & TypeFlags.Never) {
3933339339
hasReturnOfTypeNever = true;
3933439340
}
39335-
pushIfUnique(aggregatedTypes, type);
39341+
pushIfUnique(aggregatedTypes, isConstContext(expr) ? getRegularTypeOfLiteralType(type) : type);
3933639342
}
3933739343
else {
3933839344
hasReturnWithNoExpression = true;

0 commit comments

Comments
 (0)