diff --git a/src/services/codefixes/helpers.ts b/src/services/codefixes/helpers.ts
index 107f954def412..ebb58bae13c09 100644
--- a/src/services/codefixes/helpers.ts
+++ b/src/services/codefixes/helpers.ts
@@ -621,6 +621,9 @@ function endOfRequiredTypeParameters(checker: TypeChecker, type: GenericType): n
const fullTypeArguments = type.typeArguments;
const target = type.target;
for (let cutoff = 0; cutoff < fullTypeArguments.length; cutoff++) {
+ if (target.localTypeParameters?.[cutoff].constraint === undefined) {
+ continue;
+ }
const typeArguments = fullTypeArguments.slice(0, cutoff);
const filledIn = checker.fillMissingTypeArguments(typeArguments, target.typeParameters, cutoff, /*isJavaScriptImplicitAny*/ false);
if (filledIn.every((fill, i) => fill === fullTypeArguments[i])) {
diff --git a/tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports57-generics-doesnt-drop-trailing-unknown.ts b/tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports57-generics-doesnt-drop-trailing-unknown.ts
new file mode 100644
index 0000000000000..5b3e75aa13df3
--- /dev/null
+++ b/tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports57-generics-doesnt-drop-trailing-unknown.ts
@@ -0,0 +1,19 @@
+///
+
+// @isolatedDeclarations: true
+// @declaration: true
+// @lib: es2015
+////
+////let x: unknown;
+////export const s = new Set([x]);
+////
+
+verify.codeFix({
+ description: "Add annotation of type 'Set'",
+ index: 0,
+ newFileContent:
+`
+let x: unknown;
+export const s: Set = new Set([x]);
+`,
+});
diff --git a/tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports58-genercs-doesnt-drop-trailing-unknown-2.ts b/tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports58-genercs-doesnt-drop-trailing-unknown-2.ts
new file mode 100644
index 0000000000000..df71c96aad80f
--- /dev/null
+++ b/tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports58-genercs-doesnt-drop-trailing-unknown-2.ts
@@ -0,0 +1,17 @@
+///
+
+// @isolatedDeclarations: true
+// @declaration: true
+// @lib: es2015
+////
+////export const s = new Set();
+////
+
+verify.codeFix({
+ description: "Add annotation of type 'Set'",
+ index: 0,
+ newFileContent:
+`
+export const s: Set = new Set();
+`,
+});
diff --git a/tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports59-drops-unneeded-after-unknown.ts b/tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports59-drops-unneeded-after-unknown.ts
new file mode 100644
index 0000000000000..0df342ea44cd2
--- /dev/null
+++ b/tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports59-drops-unneeded-after-unknown.ts
@@ -0,0 +1,18 @@
+///
+
+// @isolatedDeclarations: true
+// @declaration: true
+////
+////export interface Foo {}
+////export function g(x: Foo) { return x; }
+////
+
+verify.codeFix({
+ description: "Add return type 'Foo'",
+ index: 0,
+ newFileContent:
+`
+export interface Foo {}
+export function g(x: Foo): Foo { return x; }
+`,
+});
\ No newline at end of file
diff --git a/tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports60-drops-unneeded-non-trailing-unknown.ts b/tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports60-drops-unneeded-non-trailing-unknown.ts
new file mode 100644
index 0000000000000..a44acbea25d4b
--- /dev/null
+++ b/tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports60-drops-unneeded-non-trailing-unknown.ts
@@ -0,0 +1,18 @@
+///
+
+// @isolatedDeclarations: true
+// @declaration: true
+////
+////export interface Foo {}
+////export function f(x: Foo) { return x; }
+////
+
+verify.codeFix({
+ description: "Add return type 'Foo'",
+ index: 0,
+ newFileContent:
+`
+export interface Foo {}
+export function f(x: Foo): Foo { return x; }
+`,
+});
\ No newline at end of file