Closed
Description
Acknowledgement
- I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.
Comment
I'm trying to programmatically use the convertFunctionToEs6Class
codefix via the TypeScript LanguageService, based on [its implementation in the TypeScript repo](https://github.com/microsoft/TypeScript/blob/main/src/services/codefixes/convertFunctionToEs6Class.ts).
I expected to get TS80002
as a suggestion diagnostic, but it's never reported, even for very standard constructor + prototype assignments.
🔢 Repro
import * as ts from 'typescript';
const fileName = 'virtual.ts';
const fileText = `
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
console.log("Hello, " + this.name);
};
`;
const files = new Map([[fileName, fileText]]);
const languageService = ts.createLanguageService({
getScriptFileNames: () => [fileName],
getScriptVersion: () => '1',
getScriptSnapshot: (f) => {
const content = files.get(f);
return content ? ts.ScriptSnapshot.fromString(content) : undefined;
},
getCurrentDirectory: () => process.cwd(),
getCompilationSettings: () => ({ target: ts.ScriptTarget.ESNext, noImplicitAny: true }),
getDefaultLibFileName: ts.getDefaultLibFilePath,
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
readDirectory: ts.sys.readDirectory,
});
const diagnostics = languageService.getSuggestionDiagnostics(fileName);
console.log({ diagnostics });
/*
{
diagnostics: [
{
file: [Object ...],
start: 17,
length: 4,
messageText: "Parameter 'name' implicitly has an 'any' type, but a better type may be inferred from usage.",
category: 2,
code: 7044,
reportsUnnecessary: undefined,
reportsDeprecated: undefined,
}
],
}
*/
⚠️ Problem
- I only get TS7044 (
Parameter 'name' implicitly has an 'any' type
) as a diagnostic. - TS80002 (
May be converted to an ES6 class
) is not returned at all, so the codefix cannot be triggered. - Yet this exact code is handled correctly by the
convertFunctionToEs6Class
logic in the compiler services source.
✅ Expected
TS80002 should be reported, and convertFunctionToEs6Class
should be available via getCodeFixesAtPosition
.
💻 Environment
- TypeScript:
~5.8.3
- Node.js:
v22.6.0
- Reproduced via
ts-node
and plain TS project setup
📝 Notes
- Possibly related to file registration or classification of suggestion diagnostics?