Type utilities for working with TypeScript + ESLint together
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
TypeScript ESLint Type Utils provides essential type utilities for working with TypeScript within ESLint rules. This package offers a comprehensive set of functions for type analysis, type checking, and TypeScript compiler API interactions designed specifically for ESLint rule authors who need to build sophisticated type-aware rules.
npm install @typescript-eslint/type-utilsimport {
isTypeReadonly,
getTypeName,
isUnsafeAssignment,
containsAllTypesByName,
isTypeAnyType,
getConstrainedTypeAtLocation
} from "@typescript-eslint/type-utils";For CommonJS:
const {
isTypeReadonly,
getTypeName,
isUnsafeAssignment,
containsAllTypesByName,
isTypeAnyType,
getConstrainedTypeAtLocation
} = require("@typescript-eslint/type-utils");import { RuleContext } from "@typescript-eslint/utils";
import { isTypeReadonly, getTypeName, isTypeAnyType } from "@typescript-eslint/type-utils";
export default {
create(context: RuleContext<string, unknown[]>) {
const services = context.parserServices;
const program = services.program;
const checker = program.getTypeChecker();
return {
VariableDeclaration(node) {
const tsNode = services.esTreeNodeToTSNodeMap.get(node);
const type = checker.getTypeAtLocation(tsNode);
// Check if type is readonly
if (isTypeReadonly(program, type)) {
console.log("Variable is readonly");
}
// Get type name
const typeName = getTypeName(checker, type);
console.log(`Type name: ${typeName}`);
// Check if type is any
if (isTypeAnyType(type)) {
context.report({
node,
messageId: "noAnyTypes"
});
}
}
};
}
};TypeScript ESLint Type Utils is organized around several key functional areas:
isTypeAnyType, isNullableType, etc.)getTypeName, containsAllTypesByName)getConstrainedTypeAtLocation, getContextualType)isUnsafeAssignment, isTypeReadonly)isPromiseLike, isErrorLike, etc.)getDeclaration, isSymbolFromDefaultLibrary)Essential type checking functions that determine characteristics of TypeScript types. These predicates help identify specific type categories and properties.
function isTypeAnyType(type: ts.Type): boolean;
function isTypeUnknownType(type: ts.Type): boolean;
function isTypeNeverType(type: ts.Type): boolean;
function isNullableType(type: ts.Type): boolean;
function isTypeArrayTypeOrUnionOfArrayTypes(type: ts.Type, checker: ts.TypeChecker): boolean;Functions for analyzing type names, structure, and relationships. Used to understand type composition and extract meaningful information from TypeScript types.
function getTypeName(typeChecker: ts.TypeChecker, type: ts.Type): string;
function containsAllTypesByName(type: ts.Type, allowAny: boolean, allowedNames: Set<string>, matchAnyInstead?: boolean): boolean;
function getTypeFlags(type: ts.Type): ts.TypeFlags;
function isTypeFlagSet(type: ts.Type, flagsToCheck: ts.TypeFlags): boolean;Tools for working with TypeScript's type constraint system and contextual typing, essential for understanding generic types and inference.
function getConstrainedTypeAtLocation(services: ParserServicesWithTypeInformation, node: TSESTree.Node): ts.Type;
function getContextualType(checker: ts.TypeChecker, node: ts.Expression): ts.Type | undefined;Functions for detecting potentially unsafe type operations, particularly around readonly properties and any type assignments.
function isTypeReadonly(program: ts.Program, type: ts.Type, options?: ReadonlynessOptions): boolean;
function isUnsafeAssignment(type: ts.Type, receiver: ts.Type, checker: ts.TypeChecker, senderNode: TSESTree.Node | null): false | { receiver: ts.Type; sender: ts.Type };
interface ReadonlynessOptions {
readonly allow?: TypeOrValueSpecifier[];
readonly treatMethodsAsReadonly?: boolean;
}Specialized predicates for identifying built-in TypeScript types like Promise, Error, and readonly utility types.
function isPromiseLike(program: ts.Program, type: ts.Type): boolean;
function isPromiseConstructorLike(program: ts.Program, type: ts.Type): boolean;
function isErrorLike(program: ts.Program, type: ts.Type): boolean;
function isReadonlyTypeLike(
program: ts.Program,
type: ts.Type,
predicate?: (subType: { aliasSymbol: ts.Symbol; aliasTypeArguments: readonly ts.Type[] } & ts.Type) => boolean
): boolean;Tools for working with TypeScript symbols, declarations, and their relationships to source files and libraries.
function getDeclaration(services: ParserServicesWithTypeInformation, node: TSESTree.Node): ts.Declaration | null;
function isSymbolFromDefaultLibrary(program: ts.Program, symbol: ts.Symbol | undefined): boolean;
function getSourceFileOfNode(node: ts.Node): ts.SourceFile;Functions for extracting and analyzing property types from complex type structures.
function getTypeOfPropertyOfName(checker: ts.TypeChecker, type: ts.Type, name: string, escapedName?: ts.__String): ts.Type | undefined;
function getTypeOfPropertyOfType(checker: ts.TypeChecker, type: ts.Type, property: ts.Symbol): ts.Type | undefined;Advanced type matching system using specifiers to identify types from specific packages, files, or the TypeScript standard library.
type TypeOrValueSpecifier = string | FileSpecifier | LibSpecifier | PackageSpecifier;
function typeMatchesSpecifier(type: ts.Type, specifier: TypeOrValueSpecifier, program: ts.Program): boolean;
function typeMatchesSomeSpecifier(type: ts.Type, specifiers: TypeOrValueSpecifier[], program: ts.Program): boolean;
function valueMatchesSpecifier(node: TSESTree.Node, specifier: TypeOrValueSpecifier, program: ts.Program, type: ts.Type): boolean;
function valueMatchesSomeSpecifier(node: TSESTree.Node, specifiers: TypeOrValueSpecifier[], program: ts.Program, type: ts.Type): boolean;interface FileSpecifier {
from: 'file';
name: string | string[];
path?: string;
}
interface LibSpecifier {
from: 'lib';
name: string | string[];
}
interface PackageSpecifier {
from: 'package';
name: string | string[];
package: string;
}
enum AnyType {
Any,
PromiseAny,
AnyArray,
Safe,
}
// Constants
const readonlynessOptionsDefaults: ReadonlynessOptions;
const readonlynessOptionsSchema: JSONSchema4;
const typeOrValueSpecifiersSchema: JSONSchema4;