Utilities for working with TypeScript + ESLint together
—
TypeScript-specific utility functions and type helpers for advanced TypeScript integration. These utilities provide type-safe operations and enhanced TypeScript development support.
Type-safe array checking that provides proper type narrowing.
/**
* Type-safe array check with proper type narrowing
* @param arg - Value to check if it's an array
* @returns Type predicate indicating if arg is an array
*/
function isArray(arg: unknown): arg is readonly unknown[];Usage Example:
import { TSUtils } from '@typescript-eslint/utils';
function processValue(value: unknown) {
if (TSUtils.isArray(value)) {
// value is now typed as readonly unknown[]
value.forEach(item => {
console.log(item);
});
// Safe array methods are available
const length = value.length;
const first = value[0];
} else {
// Handle non-array values
console.log('Not an array:', value);
}
}
// Examples
processValue([1, 2, 3]); // Handles as array
processValue('hello'); // Handles as non-array
processValue({ length: 3 }); // Handles as non-array (not a real array)Utility type for preventing TypeScript from inferring specific type parameters.
/**
* Prevents TypeScript from inferring the wrapped type parameter
* Useful when you want to explicitly specify generic types
*/
type NoInfer<T> = [T][T extends any ? 0 : never];Usage Example:
import { TSUtils } from '@typescript-eslint/utils';
// Without NoInfer - TypeScript infers T as string
function example1<T>(value: T, defaultValue: T): T {
return value ?? defaultValue;
}
example1('hello', 'world'); // T is inferred as string
// With NoInfer - T must be explicitly specified
function example2<T>(value: T, defaultValue: TSUtils.NoInfer<T>): T {
return value ?? defaultValue;
}
// Must explicitly specify the type parameter
example2<string>('hello', 'world'); // T is explicitly string
example2<number>(42, 0); // T is explicitly number
// This is useful for creating more controlled APIs
function createValidator<T>(
validate: (value: unknown) => value is T,
defaultValue: TSUtils.NoInfer<T>
): (input: unknown) => T {
return (input: unknown) => {
return validate(input) ? input : defaultValue;
};
}
// Type must be specified explicitly
const stringValidator = createValidator<string>(
(v): v is string => typeof v === 'string',
'default'
);namespace TSUtils {
/**
* Type-safe array checking function
* Provides proper type narrowing from unknown to readonly unknown[]
*/
function isArray(arg: unknown): arg is readonly unknown[];
/**
* Type utility to prevent type parameter inference
* Useful for creating APIs where types must be explicitly specified
*/
type NoInfer<T> = [T][T extends any ? 0 : never];
}TSUtils works seamlessly with other @typescript-eslint/utils components:
import { ESLintUtils, TSUtils } from '@typescript-eslint/utils';
const createRule = ESLintUtils.RuleCreator(name => `https://example.com/${name}`);
// Using NoInfer for explicit type control in rule options
type RuleOptions<TConfig> = [{
config: TSUtils.NoInfer<TConfig>;
enableChecks: boolean;
}];
export default createRule<RuleOptions<{ strict: boolean }>, MessageIds>({
// Rule implementation with explicitly typed config
});import { ASTUtils, TSUtils } from '@typescript-eslint/utils';
function analyzeNode(node: unknown) {
// First check if we have an array of nodes
if (TSUtils.isArray(node)) {
node.forEach(item => {
// Use AST utilities on each item if it's a proper node
if (typeof item === 'object' && item !== null && 'type' in item) {
const isFunction = ASTUtils.isFunction(item as any);
// Process function nodes
}
});
}
}The TSUtils provide enhanced type safety for common operations:
isArray provides proper type narrowing compared to Array.isArrayNoInfer enables more precise generic type controlThese utilities are particularly valuable when building TypeScript-aware ESLint rules and tools that need robust type checking and controlled type inference.
Install with Tessl CLI
npx tessl i tessl/npm-typescript-eslint--utils