diff --git a/src/services/codefixes/convertToUnknownFirst.ts b/src/services/codefixes/convertToUnknownFirst.ts new file mode 100644 index 0000000000000..f4f0e20e4cdd5 --- /dev/null +++ b/src/services/codefixes/convertToUnknownFirst.ts @@ -0,0 +1,21 @@ +/* @internal */ +namespace ts.codefix { + const fixId = "convertToUnknownFirst"; + + const errorCodes = [Diagnostics.Conversion_of_type_0_to_type_1_may_be_a_mistake_because_neither_type_sufficiently_overlaps_with_the_other_If_this_was_intentional_convert_the_expression_to_unknown_first]; + registerCodeFix({ + errorCodes, + getCodeActions: (context) => { + const changes = textChanges.ChangeTracker.with(context, t => changeInferToUnknown(t, context.sourceFile, context.token)); + return [createCodeFixAction(fixId, changes, Diagnostics.Replace_infer_0_with_unknown, fixId, Diagnostics.Replace_all_unused_infer_with_unknown]; + }, + fixIds: [fixId], + getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => changeInferToUnknown(changes, diag.file, diag.start)), + }); + + + // TODO how does codefix know which scenario to fire? + function changeInferToUnknown(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Node): void { + changes.replaceNode(sourceFile, token.parent, createKeywordTypeNode(SyntaxKind.UnknownKeyword)); + } +} diff --git a/tests/cases/fourslash/convertToUnknownFirst.ts b/tests/cases/fourslash/convertToUnknownFirst.ts new file mode 100644 index 0000000000000..d1ece7ddb3c03 --- /dev/null +++ b/tests/cases/fourslash/convertToUnknownFirst.ts @@ -0,0 +1,9 @@ +/// + +////0 as string; + +verify.codeFix({ + description: "Replace '0' with '0 as unknown'", + index: 0, + newFileContent: "0 as unknown as string", +});