JoshuaKGoldberg / TypeStat

Converts JavaScript to TypeScript and TypeScript to better TypeScript. 🧫

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

πŸ› Bug: noInferableTypes removes types from consts where it's not safe

rubiesonthesky opened this issue Β· comments

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.

Expected

Keep there types

// map
const incompleteTypes: TypeSummariesPerNodeByName = new Map();
// array
const mutations: Mutation[] = [];
// function
export const fixIncompleteImplicitClassGenerics: FileMutator = (
	request: FileMutationsRequest,
) => collectMutationsFromNodes(request, ts.isClassLike, visitClassLike);

Actual

This is the current output

// map
const incompleteTypes = new Map();
// array
const mutations = [];
// function
export const fixIncompleteImplicitClassGenerics = (
	request: FileMutationsRequest,
) => collectMutationsFromNodes(request, ts.isClassLike, visitClassLike);

Additional Info

Continuing dogfooding this repo to TypeStat

typestat.json

{
    "fixes": {
        "noInferableTypes": true
    },
    "include": [
        "src/**/*.{ts,tsx}"
    ],
    "projectPath": "./tsconfig.json"
}

I think the problem is here with this logic. It's not safe to say that you can remove types always from const. It's fine for primitive types like string, number or boolean. But for arrays, maps or sets it may not be safe.

const getNoInferableTypeVariableDeclarationMutation = (
	node: InferableVariableDeclaration,
	request: FileMutationsRequest,
) => {
	if (
		// `const` variables should always have their declarations removed
		tsutils.isNodeFlagSet(node.parent, ts.NodeFlags.Const) ||
		// `let` variables should have only uninformative declarations removed
		declaredInitializedTypeNodeIsRedundant(request, node.type, node.initializer)
	) {
		return createTypeRemovalMutation(request, node);
	}

	return undefined;
};