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-analysis.mddocs/

0

# Type Analysis

1

2

Functions for analyzing type names, structure, and relationships. These utilities help extract meaningful information from TypeScript types and understand type composition.

3

4

## Capabilities

5

6

### Type Name Analysis

7

8

Functions for extracting human-readable names from TypeScript types.

9

10

```typescript { .api }

11

/**

12

* Get the type name of a given type. Handles string-like types, type parameters,

13

* unions, and intersections specially.

14

*/

15

function getTypeName(typeChecker: ts.TypeChecker, type: ts.Type): string;

16

```

17

18

**Usage Examples:**

19

20

```typescript

21

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

22

23

// In an ESLint rule

24

export default {

25

create(context) {

26

const services = context.parserServices;

27

const checker = services.program.getTypeChecker();

28

29

return {

30

VariableDeclaration(node) {

31

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

32

const type = checker.getTypeAtLocation(tsNode);

33

34

const typeName = getTypeName(checker, type);

35

console.log(`Variable type: ${typeName}`);

36

37

// Example outputs:

38

// "string" for string type

39

// "number | string" for union types

40

// "{ name: string; age: number }" for object types

41

}

42

};

43

}

44

};

45

```

46

47

### Type Content Analysis

48

49

Functions for analyzing what types contain and their structure.

50

51

```typescript { .api }

52

/**

53

* Checks whether a type contains all specified type names

54

* @param type - Type being checked by name

55

* @param allowAny - Whether to consider `any` and `unknown` to match

56

* @param allowedNames - Symbol names checking on the type

57

* @param matchAnyInstead - Whether to check if any parts match rather than all parts

58

* @returns Whether the type is, extends, or contains the allowed names

59

*/

60

function containsAllTypesByName(

61

type: ts.Type,

62

allowAny: boolean,

63

allowedNames: Set<string>,

64

matchAnyInstead?: boolean

65

): boolean;

66

```

67

68

**Usage Examples:**

69

70

```typescript

71

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

72

73

// Check if a type contains specific type names

74

const allowedTypes = new Set(['string', 'number']);

75

const containsAllowed = containsAllTypesByName(

76

type,

77

false, // don't allow any/unknown

78

allowedTypes,

79

false // all parts must match

80

);

81

82

if (containsAllowed) {

83

// Type only contains string and number types

84

}

85

86

// Check if any part matches (useful for unions)

87

const hasAnyAllowed = containsAllTypesByName(

88

type,

89

false,

90

allowedTypes,

91

true // match any instead of all

92

);

93

```

94

95

### Type Flag Analysis

96

97

Functions for working with TypeScript's internal type flags system.

98

99

```typescript { .api }

100

/**

101

* Gets all of the type flags in a type, iterating through unions automatically

102

*/

103

function getTypeFlags(type: ts.Type): ts.TypeFlags;

104

105

/**

106

* Checks if specific type flags are set. Decomposes union types and checks all constituents.

107

* If type is union, checks flags of every union constituent.

108

*/

109

function isTypeFlagSet(type: ts.Type, flagsToCheck: ts.TypeFlags): boolean;

110

```

111

112

**Usage Examples:**

113

114

```typescript

115

import { getTypeFlags, isTypeFlagSet } from "@typescript-eslint/type-utils";

116

import * as ts from "typescript";

117

118

// Get all flags for a type

119

const flags = getTypeFlags(someType);

120

console.log(`Type flags: ${flags}`);

121

122

// Check for specific flags

123

if (isTypeFlagSet(someType, ts.TypeFlags.String)) {

124

// Type has string flag

125

}

126

127

if (isTypeFlagSet(someType, ts.TypeFlags.Union)) {

128

// Type is a union type

129

}

130

131

// Common flag checks

132

if (isTypeFlagSet(someType, ts.TypeFlags.Any)) {

133

context.report({ node, messageId: "noAnyType" });

134

}

135

136

if (isTypeFlagSet(someType, ts.TypeFlags.Undefined | ts.TypeFlags.Null)) {

137

// Type includes undefined or null

138

}

139

```

140

141

### Utility Functions

142

143

Additional utilities for text processing and type analysis.

144

145

```typescript { .api }

146

/**

147

* Indicates whether identifiers require the use of quotation marks when accessing

148

* property definitions and dot notation

149

*/

150

function requiresQuoting(name: string, target?: ts.ScriptTarget): boolean;

151

```

152

153

**Usage Examples:**

154

155

```typescript

156

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

157

import * as ts from "typescript";

158

159

// Check if property names need quoting

160

const needsQuotes = requiresQuoting("my-property"); // true

161

const noQuotes = requiresQuoting("myProperty"); // false

162

const withTarget = requiresQuoting("let", ts.ScriptTarget.ES5); // true (reserved word)

163

164

// Use in property access suggestions

165

if (needsQuotes) {

166

suggestion = `obj["${propertyName}"]`;

167

} else {

168

suggestion = `obj.${propertyName}`;

169

}

170

```

171

172

## Advanced Analysis Patterns

173

174

### Type Composition Analysis

175

176

```typescript

177

// Example: Analyzing complex union types

178

import { getTypeName, containsAllTypesByName, isTypeFlagSet } from "@typescript-eslint/type-utils";

179

180

function analyzeUnionType(type: ts.Type, checker: ts.TypeChecker) {

181

const typeName = getTypeName(checker, type);

182

183

if (isTypeFlagSet(type, ts.TypeFlags.Union)) {

184

// It's a union type

185

const allowedPrimitives = new Set(['string', 'number', 'boolean']);

186

187

if (containsAllTypesByName(type, false, allowedPrimitives, true)) {

188

console.log(`Union contains primitives: ${typeName}`);

189

}

190

}

191

}

192

```

193

194

### Flag-based Type Categorization

195

196

```typescript

197

// Example: Categorizing types by flags

198

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

199

import * as ts from "typescript";

200

201

function categorizeType(type: ts.Type): string {

202

const flags = getTypeFlags(type);

203

204

if (flags & ts.TypeFlags.String) return "string";

205

if (flags & ts.TypeFlags.Number) return "number";

206

if (flags & ts.TypeFlags.Boolean) return "boolean";

207

if (flags & ts.TypeFlags.Undefined) return "undefined";

208

if (flags & ts.TypeFlags.Null) return "null";

209

if (flags & ts.TypeFlags.Any) return "any";

210

if (flags & ts.TypeFlags.Unknown) return "unknown";

211

if (flags & ts.TypeFlags.Never) return "never";

212

if (flags & ts.TypeFlags.Union) return "union";

213

if (flags & ts.TypeFlags.Intersection) return "intersection";

214

215

return "complex";

216

}

217

```