Skip to content

Commit cb45ffe

Browse files
committed
Call widening functions on the correct type when adding satisfies to expressions
Widening of types only work on types from variable declaration, so when getWidenedLiteralType is being called on expressions the compiler wouldn't widen it. So call getWidenedLiteralType on the type of variables instead on those cases.
1 parent 5704f4e commit cb45ffe

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/services/codefixes/fixMissingTypeAnnotationOnExports.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ function withContext<T>(
375375

376376
if (!(isExpressionTarget || isShorthandPropertyAssignmentTarget)) return undefined;
377377

378-
const { typeNode, mutatedTarget } = inferType(targetNode);
378+
const { typeNode, mutatedTarget } = inferType(targetNode, type);
379379
if (!typeNode || mutatedTarget) return undefined;
380380

381381
if (isShorthandPropertyAssignmentTarget) {
@@ -921,7 +921,7 @@ function withContext<T>(
921921
mutatedTarget: boolean;
922922
}
923923

924-
function inferType(node: Node): InferenceResult {
924+
function inferType(node: Node, variableType?: Type | undefined): InferenceResult {
925925
if (typePrintMode === TypePrintMode.RELATIVE) {
926926
return relativeType(node);
927927
}
@@ -934,6 +934,11 @@ function withContext<T>(
934934
}
935935

936936
if (typePrintMode === TypePrintMode.WIDENED) {
937+
if (variableType) {
938+
type = variableType;
939+
}
940+
// Widening of types can happen on union of type literals on
941+
// declaration emit so we query it.
937942
const widenedType = typeChecker.getWidenedLiteralType(type);
938943
if (typeChecker.isTypeAssignableTo(widenedType, type)) {
939944
return emptyInferenceResult;

tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports36-conditional-releative.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ verify.codeFixAvailable([
2222
},
2323
{
2424
"description": "Add satisfies and an inline type assertion with 'typeof A | typeof B'"
25+
},
26+
{
27+
"description": "Add satisfies and an inline type assertion with 'string'"
2528
}
2629
])
2730
verify.codeFix({
2831
description: "Add satisfies and an inline type assertion with 'typeof A | typeof B'" ,
29-
index: 3,
32+
index: 4,
3033
newFileContent:
3134
`const A = "A"
3235
const B = "B"

0 commit comments

Comments
 (0)