or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

builtin-types.mdindex.mdproperty-types.mdsymbol-analysis.mdtype-analysis.mdtype-constraints.mdtype-predicates.mdtype-safety.mdtype-specifiers.md

type-predicates.mddocs/

0

# Type Predicates

1

2

Essential type checking functions that determine characteristics of TypeScript types. These predicates help identify specific type categories and properties for use in ESLint rules.

3

4

## Capabilities

5

6

### Basic Type Predicates

7

8

Core functions to identify fundamental TypeScript types.

9

10

```typescript { .api }

11

/**

12

* Returns true if the type is `any`

13

*/

14

function isTypeAnyType(type: ts.Type): boolean;

15

16

/**

17

* Returns true if the type is `unknown`

18

*/

19

function isTypeUnknownType(type: ts.Type): boolean;

20

21

/**

22

* Returns true if the type is `never`

23

*/

24

function isTypeNeverType(type: ts.Type): boolean;

25

26

/**

27

* Checks if the given type is (or accepts) nullable

28

*/

29

function isNullableType(type: ts.Type): boolean;

30

```

31

32

**Usage Examples:**

33

34

```typescript

35

import { isTypeAnyType, isTypeUnknownType, isNullableType } from "@typescript-eslint/type-utils";

36

37

// In an ESLint rule

38

export default {

39

create(context) {

40

const services = context.parserServices;

41

const checker = services.program.getTypeChecker();

42

43

return {

44

VariableDeclaration(node) {

45

const tsNode = services.esTreeNodeToTSNodeMap.get(node);

46

const type = checker.getTypeAtLocation(tsNode);

47

48

if (isTypeAnyType(type)) {

49

context.report({

50

node,

51

messageId: "avoidAnyType"

52

});

53

}

54

55

if (isNullableType(type)) {

56

// Handle nullable types

57

}

58

}

59

};

60

}

61

};

62

```

63

64

### Array Type Predicates

65

66

Functions for identifying array types and specific array-based types.

67

68

```typescript { .api }

69

/**

70

* Checks if the given type is either an array type or a union made up solely of array types

71

*/

72

function isTypeArrayTypeOrUnionOfArrayTypes(type: ts.Type, checker: ts.TypeChecker): boolean;

73

74

/**

75

* Returns true if the type is `any[]`

76

*/

77

function isTypeAnyArrayType(type: ts.Type, checker: ts.TypeChecker): boolean;

78

79

/**

80

* Returns true if the type is `unknown[]`

81

*/

82

function isTypeUnknownArrayType(type: ts.Type, checker: ts.TypeChecker): boolean;

83

```

84

85

**Usage Examples:**

86

87

```typescript

88

import { isTypeArrayTypeOrUnionOfArrayTypes, isTypeAnyArrayType } from "@typescript-eslint/type-utils";

89

90

// Check if a type is array-like

91

if (isTypeArrayTypeOrUnionOfArrayTypes(someType, checker)) {

92

// Handle array types

93

}

94

95

// Detect any[] types

96

if (isTypeAnyArrayType(someType, checker)) {

97

context.report({

98

node,

99

messageId: "avoidAnyArray"

100

});

101

}

102

```

103

104

### Type Reference Predicates

105

106

Type guards and predicates for specific TypeScript type structures.

107

108

```typescript { .api }

109

/**

110

* Type guard for TypeReference types

111

*/

112

function isTypeReferenceType(type: ts.Type): type is ts.TypeReference;

113

114

/**

115

* Type guard for BigIntLiteral types

116

*/

117

function isTypeBigIntLiteralType(type: ts.Type): type is ts.BigIntLiteralType;

118

119

/**

120

* Type guard for TemplateLiteral types

121

*/

122

function isTypeTemplateLiteralType(type: ts.Type): type is ts.TemplateLiteralType;

123

```

124

125

**Usage Examples:**

126

127

```typescript

128

import { isTypeReferenceType, isTypeBigIntLiteralType } from "@typescript-eslint/type-utils";

129

130

// Type guard usage

131

if (isTypeReferenceType(type)) {

132

// type is now narrowed to ts.TypeReference

133

const typeArgs = type.typeArguments;

134

}

135

136

if (isTypeBigIntLiteralType(type)) {

137

// type is now narrowed to ts.BigIntLiteralType

138

const value = type.value;

139

}

140

```

141

142

### Type Relationship Predicates

143

144

Functions for analyzing relationships between types.

145

146

```typescript { .api }

147

/**

148

* Returns whether a type is an instance of the parent type, including for the parent's base types

149

*/

150

function typeIsOrHasBaseType(type: ts.Type, parentType: ts.Type): boolean;

151

```

152

153

**Usage Examples:**

154

155

```typescript

156

import { typeIsOrHasBaseType } from "@typescript-eslint/type-utils";

157

158

// Check inheritance relationships

159

if (typeIsOrHasBaseType(derivedType, baseType)) {

160

// derivedType extends or is baseType

161

}

162

```

163

164

## Type Discrimination

165

166

### Any Type Discrimination

167

168

```typescript { .api }

169

enum AnyType {

170

Any,

171

PromiseAny,

172

AnyArray,

173

Safe,

174

}

175

176

/**

177

* Returns AnyType.Any if the type is `any`,

178

* AnyType.AnyArray if the type is `any[]` or `readonly any[]`,

179

* AnyType.PromiseAny if the type is `Promise<any>`,

180

* otherwise returns AnyType.Safe

181

*/

182

function discriminateAnyType(type: ts.Type, checker: ts.TypeChecker, program: ts.Program, tsNode: ts.Node): AnyType;

183

```

184

185

**Usage Examples:**

186

187

```typescript

188

import { discriminateAnyType, AnyType } from "@typescript-eslint/type-utils";

189

190

const anyCategory = discriminateAnyType(type, checker, program, tsNode);

191

192

switch (anyCategory) {

193

case AnyType.Any:

194

// Handle plain any type

195

break;

196

case AnyType.AnyArray:

197

// Handle any[] or readonly any[]

198

break;

199

case AnyType.PromiseAny:

200

// Handle Promise<any>

201

break;

202

case AnyType.Safe:

203

// Type is safe (not any-related)

204

break;

205

}

206

```