or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

ast-utils.mdeslint-utils.mdindex.mdjson-schema.mdts-eslint.mdts-utils.md

ts-utils.mddocs/

0

# TypeScript Utilities

1

2

TypeScript-specific utility functions and type helpers for advanced TypeScript integration. These utilities provide type-safe operations and enhanced TypeScript development support.

3

4

## Capabilities

5

6

### Array Type Checking

7

8

Type-safe array checking that provides proper type narrowing.

9

10

```typescript { .api }

11

/**

12

* Type-safe array check with proper type narrowing

13

* @param arg - Value to check if it's an array

14

* @returns Type predicate indicating if arg is an array

15

*/

16

function isArray(arg: unknown): arg is readonly unknown[];

17

```

18

19

**Usage Example:**

20

21

```typescript

22

import { TSUtils } from '@typescript-eslint/utils';

23

24

function processValue(value: unknown) {

25

if (TSUtils.isArray(value)) {

26

// value is now typed as readonly unknown[]

27

value.forEach(item => {

28

console.log(item);

29

});

30

31

// Safe array methods are available

32

const length = value.length;

33

const first = value[0];

34

} else {

35

// Handle non-array values

36

console.log('Not an array:', value);

37

}

38

}

39

40

// Examples

41

processValue([1, 2, 3]); // Handles as array

42

processValue('hello'); // Handles as non-array

43

processValue({ length: 3 }); // Handles as non-array (not a real array)

44

```

45

46

### Type Inference Control

47

48

Utility type for preventing TypeScript from inferring specific type parameters.

49

50

```typescript { .api }

51

/**

52

* Prevents TypeScript from inferring the wrapped type parameter

53

* Useful when you want to explicitly specify generic types

54

*/

55

type NoInfer<T> = [T][T extends any ? 0 : never];

56

```

57

58

**Usage Example:**

59

60

```typescript

61

import { TSUtils } from '@typescript-eslint/utils';

62

63

// Without NoInfer - TypeScript infers T as string

64

function example1<T>(value: T, defaultValue: T): T {

65

return value ?? defaultValue;

66

}

67

68

example1('hello', 'world'); // T is inferred as string

69

70

// With NoInfer - T must be explicitly specified

71

function example2<T>(value: T, defaultValue: TSUtils.NoInfer<T>): T {

72

return value ?? defaultValue;

73

}

74

75

// Must explicitly specify the type parameter

76

example2<string>('hello', 'world'); // T is explicitly string

77

example2<number>(42, 0); // T is explicitly number

78

79

// This is useful for creating more controlled APIs

80

function createValidator<T>(

81

validate: (value: unknown) => value is T,

82

defaultValue: TSUtils.NoInfer<T>

83

): (input: unknown) => T {

84

return (input: unknown) => {

85

return validate(input) ? input : defaultValue;

86

};

87

}

88

89

// Type must be specified explicitly

90

const stringValidator = createValidator<string>(

91

(v): v is string => typeof v === 'string',

92

'default'

93

);

94

```

95

96

## Complete API Reference

97

98

```typescript { .api }

99

namespace TSUtils {

100

/**

101

* Type-safe array checking function

102

* Provides proper type narrowing from unknown to readonly unknown[]

103

*/

104

function isArray(arg: unknown): arg is readonly unknown[];

105

106

/**

107

* Type utility to prevent type parameter inference

108

* Useful for creating APIs where types must be explicitly specified

109

*/

110

type NoInfer<T> = [T][T extends any ? 0 : never];

111

}

112

```

113

114

## Integration with Other Utilities

115

116

TSUtils works seamlessly with other @typescript-eslint/utils components:

117

118

### With ESLint Rules

119

120

```typescript

121

import { ESLintUtils, TSUtils } from '@typescript-eslint/utils';

122

123

const createRule = ESLintUtils.RuleCreator(name => `https://example.com/${name}`);

124

125

// Using NoInfer for explicit type control in rule options

126

type RuleOptions<TConfig> = [{

127

config: TSUtils.NoInfer<TConfig>;

128

enableChecks: boolean;

129

}];

130

131

export default createRule<RuleOptions<{ strict: boolean }>, MessageIds>({

132

// Rule implementation with explicitly typed config

133

});

134

```

135

136

### With AST Analysis

137

138

```typescript

139

import { ASTUtils, TSUtils } from '@typescript-eslint/utils';

140

141

function analyzeNode(node: unknown) {

142

// First check if we have an array of nodes

143

if (TSUtils.isArray(node)) {

144

node.forEach(item => {

145

// Use AST utilities on each item if it's a proper node

146

if (typeof item === 'object' && item !== null && 'type' in item) {

147

const isFunction = ASTUtils.isFunction(item as any);

148

// Process function nodes

149

}

150

});

151

}

152

}

153

```

154

155

## Type Safety Benefits

156

157

The TSUtils provide enhanced type safety for common operations:

158

159

1. **Array Checking**: `isArray` provides proper type narrowing compared to `Array.isArray`

160

2. **Type Control**: `NoInfer` enables more precise generic type control

161

3. **Integration**: Designed to work seamlessly with TypeScript ESLint tooling

162

163

These utilities are particularly valuable when building TypeScript-aware ESLint rules and tools that need robust type checking and controlled type inference.