Type utilities for working with TypeScript + ESLint together
—
Essential type checking functions that determine characteristics of TypeScript types. These predicates help identify specific type categories and properties for use in ESLint rules.
Core functions to identify fundamental TypeScript types.
/**
* Returns true if the type is `any`
*/
function isTypeAnyType(type: ts.Type): boolean;
/**
* Returns true if the type is `unknown`
*/
function isTypeUnknownType(type: ts.Type): boolean;
/**
* Returns true if the type is `never`
*/
function isTypeNeverType(type: ts.Type): boolean;
/**
* Checks if the given type is (or accepts) nullable
*/
function isNullableType(type: ts.Type): boolean;Usage Examples:
import { isTypeAnyType, isTypeUnknownType, isNullableType } from "@typescript-eslint/type-utils";
// In an ESLint rule
export default {
create(context) {
const services = context.parserServices;
const checker = services.program.getTypeChecker();
return {
VariableDeclaration(node) {
const tsNode = services.esTreeNodeToTSNodeMap.get(node);
const type = checker.getTypeAtLocation(tsNode);
if (isTypeAnyType(type)) {
context.report({
node,
messageId: "avoidAnyType"
});
}
if (isNullableType(type)) {
// Handle nullable types
}
}
};
}
};Functions for identifying array types and specific array-based types.
/**
* Checks if the given type is either an array type or a union made up solely of array types
*/
function isTypeArrayTypeOrUnionOfArrayTypes(type: ts.Type, checker: ts.TypeChecker): boolean;
/**
* Returns true if the type is `any[]`
*/
function isTypeAnyArrayType(type: ts.Type, checker: ts.TypeChecker): boolean;
/**
* Returns true if the type is `unknown[]`
*/
function isTypeUnknownArrayType(type: ts.Type, checker: ts.TypeChecker): boolean;Usage Examples:
import { isTypeArrayTypeOrUnionOfArrayTypes, isTypeAnyArrayType } from "@typescript-eslint/type-utils";
// Check if a type is array-like
if (isTypeArrayTypeOrUnionOfArrayTypes(someType, checker)) {
// Handle array types
}
// Detect any[] types
if (isTypeAnyArrayType(someType, checker)) {
context.report({
node,
messageId: "avoidAnyArray"
});
}Type guards and predicates for specific TypeScript type structures.
/**
* Type guard for TypeReference types
*/
function isTypeReferenceType(type: ts.Type): type is ts.TypeReference;
/**
* Type guard for BigIntLiteral types
*/
function isTypeBigIntLiteralType(type: ts.Type): type is ts.BigIntLiteralType;
/**
* Type guard for TemplateLiteral types
*/
function isTypeTemplateLiteralType(type: ts.Type): type is ts.TemplateLiteralType;Usage Examples:
import { isTypeReferenceType, isTypeBigIntLiteralType } from "@typescript-eslint/type-utils";
// Type guard usage
if (isTypeReferenceType(type)) {
// type is now narrowed to ts.TypeReference
const typeArgs = type.typeArguments;
}
if (isTypeBigIntLiteralType(type)) {
// type is now narrowed to ts.BigIntLiteralType
const value = type.value;
}Functions for analyzing relationships between types.
/**
* Returns whether a type is an instance of the parent type, including for the parent's base types
*/
function typeIsOrHasBaseType(type: ts.Type, parentType: ts.Type): boolean;Usage Examples:
import { typeIsOrHasBaseType } from "@typescript-eslint/type-utils";
// Check inheritance relationships
if (typeIsOrHasBaseType(derivedType, baseType)) {
// derivedType extends or is baseType
}enum AnyType {
Any,
PromiseAny,
AnyArray,
Safe,
}
/**
* Returns AnyType.Any if the type is `any`,
* AnyType.AnyArray if the type is `any[]` or `readonly any[]`,
* AnyType.PromiseAny if the type is `Promise<any>`,
* otherwise returns AnyType.Safe
*/
function discriminateAnyType(type: ts.Type, checker: ts.TypeChecker, program: ts.Program, tsNode: ts.Node): AnyType;Usage Examples:
import { discriminateAnyType, AnyType } from "@typescript-eslint/type-utils";
const anyCategory = discriminateAnyType(type, checker, program, tsNode);
switch (anyCategory) {
case AnyType.Any:
// Handle plain any type
break;
case AnyType.AnyArray:
// Handle any[] or readonly any[]
break;
case AnyType.PromiseAny:
// Handle Promise<any>
break;
case AnyType.Safe:
// Type is safe (not any-related)
break;
}Install with Tessl CLI
npx tessl i tessl/npm-typescript-eslint--type-utils