Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/packages/Type_Utils.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
id: type-utils
sidebar_label: type-utils
---

# `@typescript-eslint/type-utils`

<PackageLink packageName="type-utils" scope="@typescript-eslint" />

> Type utilities for working with TypeScript within ESLint rules. ✨

This package contains public utilities for working with TypeScript types that ESLint rules often use.
Rules declared in [`@typescript-eslint/eslint-plugin`](./ESLint_Plugin.mdx) use these utility functions.

The utilities in this package are both:

- More generally ESLint-focused than the broad TypeScript utilities in [`ts-api-utils`](https://npmjs.com/package/ts-api-utils)
- Separated from [`@typescript-eslint/utils`](./Utils.mdx) so that that package does not require a dependency on `typescript`

> See [Custom Rules](../developers/Custom_Rules.mdx) for documentation on creating your own custom ESLint rules for TypeScript code.
33 changes: 24 additions & 9 deletions packages/type-utils/src/builtinSymbolLikes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ import * as ts from 'typescript';
import { isSymbolFromDefaultLibrary } from './isSymbolFromDefaultLibrary';

/**
* class Foo extends Promise<number> {}
* Foo.reject
* ^ PromiseLike
* @example
* ```ts
* class DerivedClass extends Promise<number> {}
* DerivedClass.reject
* // ^ PromiseLike
* ```
*/
export function isPromiseLike(program: ts.Program, type: ts.Type): boolean {
return isBuiltinSymbolLike(program, type, 'Promise');
}

/**
* const foo = Promise
* foo.reject
* ^ PromiseConstructorLike
* @example
* ```ts
* const value = Promise
* value.reject
* // ^ PromiseConstructorLike
* ```
*/
export function isPromiseConstructorLike(
program: ts.Program,
Expand All @@ -24,17 +30,23 @@ export function isPromiseConstructorLike(
}

/**
* @example
* ```ts
* class Foo extends Error {}
* new Foo()
* ^ ErrorLike
* // ^ ErrorLike
* ```
*/
export function isErrorLike(program: ts.Program, type: ts.Type): boolean {
return isBuiltinSymbolLike(program, type, 'Error');
}

/**
* @example
* ```ts
* type T = Readonly<Error>
* ^ ReadonlyErrorLike
* // ^ ReadonlyErrorLike
* ```
*/
export function isReadonlyErrorLike(
program: ts.Program,
Expand All @@ -50,8 +62,11 @@ export function isReadonlyErrorLike(
}

/**
* @example
* ```ts
* type T = Readonly<{ foo: 'bar' }>
* ^ ReadonlyTypeLike
* // ^ ReadonlyTypeLike
* ```
*/
export function isReadonlyTypeLike(
program: ts.Program,
Expand Down
1 change: 1 addition & 0 deletions packages/website/sidebars/sidebar.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ module.exports = {
'packages/parser',
'packages/rule-tester',
'packages/scope-manager',
'packages/type-utils',
'packages/typescript-estree',
'packages/typescript-eslint',
'packages/utils',
Expand Down