From ca354f235579b8aa84012ab0664ce43f7e0da0e0 Mon Sep 17 00:00:00 2001 From: Rebecca Stevens Date: Tue, 7 Mar 2023 14:44:49 +1300 Subject: [PATCH] docs: add examples (#127) --- package.json | 3 +- pnpm-lock.yaml | 12 +- src/comments.ts | 9 + src/compilerOptions.ts | 31 ++ src/flags.ts | 45 +++ src/modifiers.ts | 7 + src/nodes/typeGuards/compound.ts | 100 ++++++ src/nodes/typeGuards/single.ts | 550 +++++++++++++++++++++++++++++ src/nodes/typeGuards/union.ts | 570 +++++++++++++++++++++++++++++++ src/scopes.ts | 9 + src/syntax.ts | 19 ++ src/tokens.ts | 10 + src/types/getters.ts | 23 ++ src/types/typeGuards/compound.ts | 53 ++- src/types/typeGuards/objects.ts | 27 ++ src/types/typeGuards/simple.ts | 162 +++++++++ src/types/utilities.ts | 81 +++++ typedoc.json | 10 + 18 files changed, 1714 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index ab06bbcd..c91e9838 100644 --- a/package.json +++ b/package.json @@ -82,8 +82,9 @@ "sentences-per-line": "^0.2.1", "should-semantic-release": "^0.0.4", "tsup": "^6.5.0", - "typedoc": "^0.23.25", + "typedoc": "^0.23.26", "typedoc-plugin-coverage": "^2.0.0", + "typedoc-plugin-custom-validation": "^1.1.0", "typedoc-plugin-mdn-links": "^2.0.2", "typedoc-plugin-versions": "^0.2.3", "typescript": "4.9.5", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cea9d4ed..9e30e124 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -34,8 +34,9 @@ specifiers: sentences-per-line: ^0.2.1 should-semantic-release: ^0.0.4 tsup: ^6.5.0 - typedoc: ^0.23.25 + typedoc: ^0.23.26 typedoc-plugin-coverage: ^2.0.0 + typedoc-plugin-custom-validation: ^1.1.0 typedoc-plugin-mdn-links: ^2.0.2 typedoc-plugin-versions: ^0.2.3 typescript: 4.9.5 @@ -77,6 +78,7 @@ devDependencies: tsup: 6.6.3_typescript@4.9.5 typedoc: 0.23.26_typescript@4.9.5 typedoc-plugin-coverage: 2.0.0_typedoc@0.23.26 + typedoc-plugin-custom-validation: 1.1.0_typedoc@0.23.26 typedoc-plugin-mdn-links: 2.0.2_typedoc@0.23.26 typedoc-plugin-versions: 0.2.3_typedoc@0.23.26 typescript: 4.9.5 @@ -7052,6 +7054,14 @@ packages: typedoc: 0.23.26_typescript@4.9.5 dev: true + /typedoc-plugin-custom-validation/1.1.0_typedoc@0.23.26: + resolution: {integrity: sha512-W3zEPgAuXXqXWoZtDEYdPQHXWVp9NfqPzQLi7uUSkrEL7LNODYaCLM2EyITzWK6JZqbr4B+nj8g0Yaofp/AvHw==} + peerDependencies: + typedoc: ^0.23.26 + dependencies: + typedoc: 0.23.26_typescript@4.9.5 + dev: true + /typedoc-plugin-mdn-links/2.0.2_typedoc@0.23.26: resolution: {integrity: sha512-Fzjvfsj3rxvmZNqWRvq9JTGBkOkrPp0kBtvJCJ4U5Jm14OF1KoRErtmwgVQcPLA5Xs8h5I/W4uZBaL8SDHsgxQ==} peerDependencies: diff --git a/src/comments.ts b/src/comments.ts index 9fd0951d..0c97410b 100644 --- a/src/comments.ts +++ b/src/comments.ts @@ -67,6 +67,15 @@ export type ForEachCommentCallback = ( * Iterates over all comments owned by `node` or its children. * * @category Nodes - Other Utilities + * + * @example + * ```ts + * declare const node: ts.Node; + * + * tsutils.forEachComment(node, (fullText, comment) => { + * console.log(`Found comment at position ${comment.pos}: '${fullText}'.`); + * }); + * ``` */ export function forEachComment( node: ts.Node, diff --git a/src/compilerOptions.ts b/src/compilerOptions.ts index 19dbd0dc..70e18c31 100644 --- a/src/compilerOptions.ts +++ b/src/compilerOptions.ts @@ -23,6 +23,16 @@ export type BooleanCompilerOptions = keyof { * This function only handles boolean flags. * * @category Compiler Options + * + * @example + * ```ts + * const options = { + * allowJs: true, + * }; + * + * isCompilerOptionEnabled(options, "allowJs"); // true + * isCompilerOptionEnabled(options, "allowSyntheticDefaultImports"); // false + * ``` */ export function isCompilerOptionEnabled( options: ts.CompilerOptions, @@ -105,6 +115,27 @@ export type StrictCompilerOption = * (except `strictPropertyInitialization`) have been enabled by `strict: true`. * * @category Compiler Options + * + * @example + * ```ts + * const optionsLenient = { + * noImplicitAny: true, + * }; + * + * isStrictCompilerOptionEnabled(optionsLenient, "noImplicitAny"); // true + * isStrictCompilerOptionEnabled(optionsLenient, "noImplicitThis"); // false + * ``` + * + * @example + * ```ts + * const optionsStrict = { + * noImplicitThis: false, + * strict: true, + * }; + * + * isStrictCompilerOptionEnabled(optionsStrict, "noImplicitAny"); // true + * isStrictCompilerOptionEnabled(optionsStrict, "noImplicitThis"); // false + * ``` */ export function isStrictCompilerOptionEnabled( options: ts.CompilerOptions, diff --git a/src/flags.ts b/src/flags.ts index debd9149..230e10ff 100644 --- a/src/flags.ts +++ b/src/flags.ts @@ -25,6 +25,15 @@ function isFlagSetOnObject(obj: { flags: number }, flag: number): boolean { * Test if the given node has the given `ModifierFlags` set. * * @category Nodes - Flag Utilities + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isModifierFlagSet(node, ts.ModifierFlags.Abstract)) { + * // ... + * } + * ``` */ export function isModifierFlagSet( node: ts.Declaration, @@ -37,6 +46,15 @@ export function isModifierFlagSet( * Test if the given node has the given `NodeFlags` set. * * @category Nodes - Flag Utilities + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isNodeFlagSet(node, ts.NodeFlags.AwaitContext)) { + * // ... + * } + * ``` */ export const isNodeFlagSet: (node: ts.Node, flag: ts.NodeFlags) => boolean = isFlagSetOnObject; @@ -45,6 +63,15 @@ export const isNodeFlagSet: (node: ts.Node, flag: ts.NodeFlags) => boolean = * Test if the given node has the given `ObjectFlags` set. * * @category Nodes - Flag Utilities + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isObjectFlagSet(node, ts.ObjectFlags.Anonymous)) { + * // ... + * } + * ``` */ export function isObjectFlagSet( objectType: ts.ObjectType, @@ -57,6 +84,15 @@ export function isObjectFlagSet( * Test if the given node has the given `SymbolFlags` set. * * @category Nodes - Flag Utilities + * + * @example + * ```ts + * declare const symbol: ts.Symbol; + * + * if (isSymbolFlagSet(symbol, ts.SymbolFlags.Accessor)) { + * // ... + * } + * ``` */ export const isSymbolFlagSet: ( symbol: ts.Symbol, @@ -67,6 +103,15 @@ export const isSymbolFlagSet: ( * Test if the given node has the given `TypeFlags` set. * * @category Nodes - Flag Utilities + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isTypeFlagSet(type, ts.TypeFlags.Any)) { + * // ... + * } + * ``` */ export const isTypeFlagSet: (type: ts.Type, flag: ts.TypeFlags) => boolean = isFlagSetOnObject; diff --git a/src/modifiers.ts b/src/modifiers.ts index 2e2fa511..2ff1fdac 100644 --- a/src/modifiers.ts +++ b/src/modifiers.ts @@ -7,6 +7,13 @@ import * as ts from "typescript"; * Test if the given iterable includes a modifier of any of the given kinds. * * @category Modifier Utilities + * + * @example + * ```ts + * declare const modifiers: ts.Modifier[]; + * + * hasModifier(modifiers, ts.SyntaxKind.AbstractKeyword); + * ``` */ export function includesModifier( modifiers: Iterable | undefined, diff --git a/src/nodes/typeGuards/compound.ts b/src/nodes/typeGuards/compound.ts index ff0b19e6..82465d02 100644 --- a/src/nodes/typeGuards/compound.ts +++ b/src/nodes/typeGuards/compound.ts @@ -32,6 +32,16 @@ export type ConstAssertionIdentifier = ts.Identifier & { * Test if a node is a {@link ConstAssertionExpression}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isConstAssertionExpression(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a {@link ConstAssertionExpression}. */ export function isConstAssertionExpression( @@ -48,6 +58,16 @@ export function isConstAssertionExpression( * Test if a node is an `IterationStatement`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isIterationStatement(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `IterationStatement`. */ export function isIterationStatement( @@ -69,6 +89,16 @@ export function isIterationStatement( * Test if a node is a `JSDocNamespaceDeclaration`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isJSDocNamespaceDeclaration(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `JSDocNamespaceDeclaration`. */ export function isJSDocNamespaceDeclaration( @@ -85,6 +115,16 @@ export function isJSDocNamespaceDeclaration( * Test if a node is a `JsxTagNamePropertyAccess`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isJsxTagNamePropertyAccess(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `JsxTagNamePropertyAccess`. */ export function isJsxTagNamePropertyAccess( @@ -109,6 +149,16 @@ export interface NamedDeclarationWithName extends ts.NamedDeclaration { * Test if a node is a {@link NamedDeclarationWithName}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isNamedDeclarationWithName(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a {@link NamedDeclarationWithName}. */ export function isNamedDeclarationWithName( @@ -126,6 +176,16 @@ export function isNamedDeclarationWithName( * Test if a node is a `NamespaceDeclaration`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isNamespaceDeclaration(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `NamespaceDeclaration`. */ export function isNamespaceDeclaration( @@ -152,6 +212,16 @@ export type NumericOrStringLikeLiteral = * Test if a node is a {@link NumericOrStringLikeLiteral}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isNumericOrStringLikeLiteral(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a {@link NumericOrStringLikeLiteral}. */ export function isNumericOrStringLikeLiteral( @@ -171,6 +241,16 @@ export function isNumericOrStringLikeLiteral( * Test if a node is a `PropertyAccessEntityNameExpression`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isPropertyAccessEntityNameExpression(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `PropertyAccessEntityNameExpression`. */ export function isPropertyAccessEntityNameExpression( @@ -187,6 +267,16 @@ export function isPropertyAccessEntityNameExpression( * Test if a node is a `SuperElementAccessExpression`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isSuperElementAccessExpression(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `SuperElementAccessExpression`. */ export function isSuperElementAccessExpression( @@ -201,6 +291,16 @@ export function isSuperElementAccessExpression( * Test if a node is a `SuperPropertyAccessExpression`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isSuperPropertyAccessExpression(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `SuperPropertyAccessExpression`. */ export function isSuperPropertyAccessExpression( diff --git a/src/nodes/typeGuards/single.ts b/src/nodes/typeGuards/single.ts index c8240f71..a448ee2c 100644 --- a/src/nodes/typeGuards/single.ts +++ b/src/nodes/typeGuards/single.ts @@ -123,6 +123,16 @@ export type VoidKeyword = ts.KeywordToken; * Test if a node is an `AbstractKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isAbstractKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `AbstractKeyword`. */ export function isAbstractKeyword(node: ts.Node): node is ts.AbstractKeyword { @@ -133,6 +143,16 @@ export function isAbstractKeyword(node: ts.Node): node is ts.AbstractKeyword { * Test if a node is an `AccessorKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isAccessorKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `AccessorKeyword`. */ export function isAccessorKeyword(node: ts.Node): node is ts.AccessorKeyword { @@ -143,6 +163,16 @@ export function isAccessorKeyword(node: ts.Node): node is ts.AccessorKeyword { * Test if a node is an {@link AnyKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isAnyKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an {@link AnyKeyword}. */ export function isAnyKeyword(node: ts.Node): node is AnyKeyword { @@ -153,6 +183,16 @@ export function isAnyKeyword(node: ts.Node): node is AnyKeyword { * Test if a node is an `AssertKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isAssertKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `AssertKeyword`. */ export function isAssertKeyword(node: ts.Node): node is ts.AssertKeyword { @@ -163,6 +203,16 @@ export function isAssertKeyword(node: ts.Node): node is ts.AssertKeyword { * Test if a node is an `AssertsKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isAssertsKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `AssertsKeyword`. */ export function isAssertsKeyword(node: ts.Node): node is ts.AssertsKeyword { @@ -173,6 +223,16 @@ export function isAssertsKeyword(node: ts.Node): node is ts.AssertsKeyword { * Test if a node is an `AsyncKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isAsyncKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `AsyncKeyword`. */ export function isAsyncKeyword(node: ts.Node): node is ts.AsyncKeyword { @@ -183,6 +243,16 @@ export function isAsyncKeyword(node: ts.Node): node is ts.AsyncKeyword { * Test if a node is an `AwaitKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isAwaitKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `AwaitKeyword`. */ export function isAwaitKeyword(node: ts.Node): node is ts.AwaitKeyword { @@ -193,6 +263,16 @@ export function isAwaitKeyword(node: ts.Node): node is ts.AwaitKeyword { * Test if a node is a {@link BigIntKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isBigIntKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a {@link BigIntKeyword}. */ export function isBigIntKeyword(node: ts.Node): node is BigIntKeyword { @@ -203,6 +283,16 @@ export function isBigIntKeyword(node: ts.Node): node is BigIntKeyword { * Test if a node is a {@link BooleanKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isBooleanKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a {@link BooleanKeyword}. */ export function isBooleanKeyword(node: ts.Node): node is BooleanKeyword { @@ -213,6 +303,16 @@ export function isBooleanKeyword(node: ts.Node): node is BooleanKeyword { * Test if a node is a `ColonToken`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isColonToken(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `ColonToken`. */ export function isColonToken(node: ts.Node): node is ts.ColonToken { @@ -223,6 +323,16 @@ export function isColonToken(node: ts.Node): node is ts.ColonToken { * Test if a node is a `ConstKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isConstKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `ConstKeyword`. */ export function isConstKeyword(node: ts.Node): node is ts.ConstKeyword { @@ -233,6 +343,16 @@ export function isConstKeyword(node: ts.Node): node is ts.ConstKeyword { * Test if a node is a `DeclareKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isDeclareKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `DeclareKeyword`. */ export function isDeclareKeyword(node: ts.Node): node is ts.DeclareKeyword { @@ -243,6 +363,16 @@ export function isDeclareKeyword(node: ts.Node): node is ts.DeclareKeyword { * Test if a node is a `DefaultKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isDefaultKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `DefaultKeyword`. */ export function isDefaultKeyword(node: ts.Node): node is ts.DefaultKeyword { @@ -253,6 +383,16 @@ export function isDefaultKeyword(node: ts.Node): node is ts.DefaultKeyword { * Test if a node is a `DotToken`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isDotToken(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `DotToken`. */ export function isDotToken(node: ts.Node): node is ts.DotToken { @@ -263,6 +403,16 @@ export function isDotToken(node: ts.Node): node is ts.DotToken { * Test if a node is an `EndOfFileToken`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isEndOfFileToken(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `EndOfFileToken`. */ export function isEndOfFileToken(node: ts.Node): node is ts.EndOfFileToken { @@ -273,6 +423,16 @@ export function isEndOfFileToken(node: ts.Node): node is ts.EndOfFileToken { * Test if a node is an `EqualsGreaterThanToken`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isEqualsGreaterThanToken(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `EqualsGreaterThanToken`. */ export function isEqualsGreaterThanToken( @@ -285,6 +445,16 @@ export function isEqualsGreaterThanToken( * Test if a node is an `EqualsToken`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isEqualsToken(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `EqualsToken`. */ export function isEqualsToken(node: ts.Node): node is ts.EqualsToken { @@ -295,6 +465,16 @@ export function isEqualsToken(node: ts.Node): node is ts.EqualsToken { * Test if a node is an `ExclamationToken`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isExclamationToken(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `ExclamationToken`. */ export function isExclamationToken(node: ts.Node): node is ts.ExclamationToken { @@ -305,6 +485,16 @@ export function isExclamationToken(node: ts.Node): node is ts.ExclamationToken { * Test if a node is an `ExportKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isExportKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `ExportKeyword`. */ export function isExportKeyword(node: ts.Node): node is ts.ExportKeyword { @@ -315,6 +505,16 @@ export function isExportKeyword(node: ts.Node): node is ts.ExportKeyword { * Test if a node is a {@link FalseKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isFalseKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a {@link FalseKeyword}. */ export function isFalseKeyword(node: ts.Node): node is FalseKeyword { @@ -325,6 +525,16 @@ export function isFalseKeyword(node: ts.Node): node is FalseKeyword { * Test if a node is a `FalseLiteral`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isFalseLiteral(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `FalseLiteral`. */ export function isFalseLiteral(node: ts.Node): node is ts.FalseLiteral { @@ -335,6 +545,16 @@ export function isFalseLiteral(node: ts.Node): node is ts.FalseLiteral { * Test if a node is an `ImportExpression`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isImportExpression(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `ImportExpression`. */ export function isImportExpression(node: ts.Node): node is ts.ImportExpression { @@ -345,6 +565,16 @@ export function isImportExpression(node: ts.Node): node is ts.ImportExpression { * Test if a node is an {@link ImportKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isImportKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an {@link ImportKeyword}. */ export function isImportKeyword(node: ts.Node): node is ImportKeyword { @@ -355,6 +585,16 @@ export function isImportKeyword(node: ts.Node): node is ImportKeyword { * Test if a node is an `InKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isInKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `InKeyword`. */ export function isInKeyword(node: ts.Node): node is ts.InKeyword { @@ -365,6 +605,16 @@ export function isInKeyword(node: ts.Node): node is ts.InKeyword { * Test if a node is an `InputFiles`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isInputFiles(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `InputFiles`. */ export function isInputFiles(node: ts.Node): node is ts.InputFiles { @@ -375,6 +625,16 @@ export function isInputFiles(node: ts.Node): node is ts.InputFiles { * Test if a node is a `JSDocText`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isJSDocText(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `JSDocText`. */ export function isJSDocText(node: ts.Node): node is ts.JSDocText { @@ -385,6 +645,16 @@ export function isJSDocText(node: ts.Node): node is ts.JSDocText { * Test if a node is a `JsonMinusNumericLiteral`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isJsonMinusNumericLiteral(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `JsonMinusNumericLiteral`. */ export function isJsonMinusNumericLiteral( @@ -397,6 +667,16 @@ export function isJsonMinusNumericLiteral( * Test if a node is a {@link NeverKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isNeverKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a {@link NeverKeyword}. */ export function isNeverKeyword(node: ts.Node): node is NeverKeyword { @@ -407,6 +687,16 @@ export function isNeverKeyword(node: ts.Node): node is NeverKeyword { * Test if a node is a {@link NullKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isNullKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a {@link NullKeyword}. */ export function isNullKeyword(node: ts.Node): node is NullKeyword { @@ -417,6 +707,16 @@ export function isNullKeyword(node: ts.Node): node is NullKeyword { * Test if a node is a `NullLiteral`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isNullLiteral(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `NullLiteral`. */ export function isNullLiteral(node: ts.Node): node is ts.NullLiteral { @@ -427,6 +727,16 @@ export function isNullLiteral(node: ts.Node): node is ts.NullLiteral { * Test if a node is a {@link NumberKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isNumberKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a {@link NumberKeyword}. */ export function isNumberKeyword(node: ts.Node): node is NumberKeyword { @@ -437,6 +747,16 @@ export function isNumberKeyword(node: ts.Node): node is NumberKeyword { * Test if a node is an {@link ObjectKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isObjectKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an {@link ObjectKeyword}. */ export function isObjectKeyword(node: ts.Node): node is ObjectKeyword { @@ -447,6 +767,16 @@ export function isObjectKeyword(node: ts.Node): node is ObjectKeyword { * Test if a node is an `OutKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isOutKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `OutKeyword`. */ export function isOutKeyword(node: ts.Node): node is ts.OutKeyword { @@ -457,6 +787,16 @@ export function isOutKeyword(node: ts.Node): node is ts.OutKeyword { * Test if a node is an `OverrideKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isOverrideKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `OverrideKeyword`. */ export function isOverrideKeyword(node: ts.Node): node is ts.OverrideKeyword { @@ -467,6 +807,16 @@ export function isOverrideKeyword(node: ts.Node): node is ts.OverrideKeyword { * Test if a node is a `PrivateKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isPrivateKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `PrivateKeyword`. */ export function isPrivateKeyword(node: ts.Node): node is ts.PrivateKeyword { @@ -477,6 +827,16 @@ export function isPrivateKeyword(node: ts.Node): node is ts.PrivateKeyword { * Test if a node is a `ProtectedKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isProtectedKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `ProtectedKeyword`. */ export function isProtectedKeyword(node: ts.Node): node is ts.ProtectedKeyword { @@ -487,6 +847,16 @@ export function isProtectedKeyword(node: ts.Node): node is ts.ProtectedKeyword { * Test if a node is a `PublicKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isPublicKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `PublicKeyword`. */ export function isPublicKeyword(node: ts.Node): node is ts.PublicKeyword { @@ -497,6 +867,16 @@ export function isPublicKeyword(node: ts.Node): node is ts.PublicKeyword { * Test if a node is a `QuestionDotToken`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isQuestionDotToken(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `QuestionDotToken`. */ export function isQuestionDotToken(node: ts.Node): node is ts.QuestionDotToken { @@ -507,6 +887,16 @@ export function isQuestionDotToken(node: ts.Node): node is ts.QuestionDotToken { * Test if a node is a `QuestionToken`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isQuestionToken(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `QuestionToken`. */ export function isQuestionToken(node: ts.Node): node is ts.QuestionToken { @@ -517,6 +907,16 @@ export function isQuestionToken(node: ts.Node): node is ts.QuestionToken { * Test if a node is a `ReadonlyKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isReadonlyKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `ReadonlyKeyword`. */ export function isReadonlyKeyword(node: ts.Node): node is ts.ReadonlyKeyword { @@ -527,6 +927,16 @@ export function isReadonlyKeyword(node: ts.Node): node is ts.ReadonlyKeyword { * Test if a node is a `StaticKeyword`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isStaticKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `StaticKeyword`. */ export function isStaticKeyword(node: ts.Node): node is ts.StaticKeyword { @@ -537,6 +947,16 @@ export function isStaticKeyword(node: ts.Node): node is ts.StaticKeyword { * Test if a node is a {@link StringKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isStringKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a {@link StringKeyword}. */ export function isStringKeyword(node: ts.Node): node is StringKeyword { @@ -547,6 +967,16 @@ export function isStringKeyword(node: ts.Node): node is StringKeyword { * Test if a node is a `SuperExpression`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isSuperExpression(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `SuperExpression`. */ export function isSuperExpression(node: ts.Node): node is ts.SuperExpression { @@ -557,6 +987,16 @@ export function isSuperExpression(node: ts.Node): node is ts.SuperExpression { * Test if a node is a {@link SuperKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isSuperKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a {@link SuperKeyword}. */ export function isSuperKeyword(node: ts.Node): node is SuperKeyword { @@ -567,6 +1007,16 @@ export function isSuperKeyword(node: ts.Node): node is SuperKeyword { * Test if a node is a {@link SymbolKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isSymbolKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a {@link SymbolKeyword}. */ export function isSymbolKeyword(node: ts.Node): node is SymbolKeyword { @@ -577,6 +1027,16 @@ export function isSymbolKeyword(node: ts.Node): node is SymbolKeyword { * Test if a node is a `SyntaxList`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isSyntaxList(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `SyntaxList`. */ export function isSyntaxList(node: ts.Node): node is ts.SyntaxList { @@ -587,6 +1047,16 @@ export function isSyntaxList(node: ts.Node): node is ts.SyntaxList { * Test if a node is a `ThisExpression`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isThisExpression(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `ThisExpression`. */ export function isThisExpression(node: ts.Node): node is ts.ThisExpression { @@ -597,6 +1067,16 @@ export function isThisExpression(node: ts.Node): node is ts.ThisExpression { * Test if a node is a {@link ThisKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isThisKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a {@link ThisKeyword}. */ export function isThisKeyword(node: ts.Node): node is ThisKeyword { @@ -607,6 +1087,16 @@ export function isThisKeyword(node: ts.Node): node is ThisKeyword { * Test if a node is a {@link TrueKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isTrueKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a {@link TrueKeyword}. */ export function isTrueKeyword(node: ts.Node): node is TrueKeyword { @@ -617,6 +1107,16 @@ export function isTrueKeyword(node: ts.Node): node is TrueKeyword { * Test if a node is a `TrueLiteral`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isTrueLiteral(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `TrueLiteral`. */ export function isTrueLiteral(node: ts.Node): node is ts.TrueLiteral { @@ -627,6 +1127,16 @@ export function isTrueLiteral(node: ts.Node): node is ts.TrueLiteral { * Test if a node is an {@link UndefinedKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isUndefinedKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an {@link UndefinedKeyword}. */ export function isUndefinedKeyword(node: ts.Node): node is UndefinedKeyword { @@ -637,6 +1147,16 @@ export function isUndefinedKeyword(node: ts.Node): node is UndefinedKeyword { * Test if a node is an {@link UnknownKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isUnknownKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an {@link UnknownKeyword}. */ export function isUnknownKeyword(node: ts.Node): node is UnknownKeyword { @@ -647,6 +1167,16 @@ export function isUnknownKeyword(node: ts.Node): node is UnknownKeyword { * Test if a node is an `UnparsedPrologue`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isUnparsedPrologue(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `UnparsedPrologue`. */ export function isUnparsedPrologue(node: ts.Node): node is ts.UnparsedPrologue { @@ -657,6 +1187,16 @@ export function isUnparsedPrologue(node: ts.Node): node is ts.UnparsedPrologue { * Test if a node is an `UnparsedSyntheticReference`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isUnparsedSyntheticReference(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `UnparsedSyntheticReference`. */ export function isUnparsedSyntheticReference( @@ -669,6 +1209,16 @@ export function isUnparsedSyntheticReference( * Test if a node is a {@link VoidKeyword}. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isVoidKeyword(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a {@link VoidKeyword}. */ export function isVoidKeyword(node: ts.Node): node is VoidKeyword { diff --git a/src/nodes/typeGuards/union.ts b/src/nodes/typeGuards/union.ts index d85f9b39..198b28a0 100644 --- a/src/nodes/typeGuards/union.ts +++ b/src/nodes/typeGuards/union.ts @@ -29,6 +29,16 @@ import { * Test if a node is an `AccessExpression`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isAccessExpression(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `AccessExpression`. */ export function isAccessExpression(node: ts.Node): node is ts.AccessExpression { @@ -41,6 +51,16 @@ export function isAccessExpression(node: ts.Node): node is ts.AccessExpression { * Test if a node is an `AccessibilityModifier`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isAccessibilityModifier(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `AccessibilityModifier`. */ export function isAccessibilityModifier( @@ -55,6 +75,16 @@ export function isAccessibilityModifier( * Test if a node is an `AccessorDeclaration`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isAccessorDeclaration(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `AccessorDeclaration`. */ export function isAccessorDeclaration( @@ -67,6 +97,16 @@ export function isAccessorDeclaration( * Test if a node is an `ArrayBindingElement`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isArrayBindingElement(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `ArrayBindingElement`. */ export function isArrayBindingElement( @@ -79,6 +119,16 @@ export function isArrayBindingElement( * Test if a node is an `ArrayBindingOrAssignmentPattern`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isArrayBindingOrAssignmentPattern(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `ArrayBindingOrAssignmentPattern`. */ export function isArrayBindingOrAssignmentPattern( @@ -91,6 +141,16 @@ export function isArrayBindingOrAssignmentPattern( * Test if a node is an `AssignmentPattern`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isAssignmentPattern(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `AssignmentPattern`. */ export function isAssignmentPattern( @@ -105,6 +165,16 @@ export function isAssignmentPattern( * Test if a node is a `BindingOrAssignmentElementRestIndicator`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isBindingOrAssignmentElementRestIndicator(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `BindingOrAssignmentElementRestIndicator`. */ export function isBindingOrAssignmentElementRestIndicator( @@ -125,6 +195,16 @@ export function isBindingOrAssignmentElementRestIndicator( * Test if a node is a `BindingOrAssignmentElementTarget`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isBindingOrAssignmentElementTarget(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `BindingOrAssignmentElementTarget`. */ export function isBindingOrAssignmentElementTarget( @@ -143,6 +223,16 @@ export function isBindingOrAssignmentElementTarget( * Test if a node is a `BindingOrAssignmentPattern`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isBindingOrAssignmentPattern(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `BindingOrAssignmentPattern`. */ export function isBindingOrAssignmentPattern( @@ -158,6 +248,16 @@ export function isBindingOrAssignmentPattern( * Test if a node is a `BindingPattern`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isBindingPattern(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `BindingPattern`. */ export function isBindingPattern(node: ts.Node): node is ts.BindingPattern { @@ -168,6 +268,16 @@ export function isBindingPattern(node: ts.Node): node is ts.BindingPattern { * Test if a node is a `BlockLike`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isBlockLike(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `BlockLike`. */ export function isBlockLike(node: ts.Node): node is ts.BlockLike { @@ -183,6 +293,16 @@ export function isBlockLike(node: ts.Node): node is ts.BlockLike { * Test if a node is a `BooleanLiteral`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isBooleanLiteral(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `BooleanLiteral`. */ export function isBooleanLiteral(node: ts.Node): node is ts.BooleanLiteral { @@ -193,6 +313,16 @@ export function isBooleanLiteral(node: ts.Node): node is ts.BooleanLiteral { * Test if a node is a `ClassLikeDeclaration`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isClassLikeDeclaration(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `ClassLikeDeclaration`. */ export function isClassLikeDeclaration( @@ -205,6 +335,16 @@ export function isClassLikeDeclaration( * Test if a node is a `ClassMemberModifier`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isClassMemberModifier(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `ClassMemberModifier`. */ export function isClassMemberModifier( @@ -222,6 +362,16 @@ export function isClassMemberModifier( * Test if a node is a `DeclarationName`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isDeclarationName(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `DeclarationName`. */ export function isDeclarationName(node: ts.Node): node is ts.DeclarationName { @@ -241,6 +391,16 @@ export function isDeclarationName(node: ts.Node): node is ts.DeclarationName { * Test if a node is a `DeclarationWithTypeParameterChildren`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isDeclarationWithTypeParameterChildren(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `DeclarationWithTypeParameterChildren`. */ export function isDeclarationWithTypeParameterChildren( @@ -259,6 +419,16 @@ export function isDeclarationWithTypeParameterChildren( * Test if a node is a `DeclarationWithTypeParameters`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isDeclarationWithTypeParameters(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `DeclarationWithTypeParameters`. */ export function isDeclarationWithTypeParameters( @@ -276,6 +446,16 @@ export function isDeclarationWithTypeParameters( * Test if a node is a `DestructuringPattern`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isDestructuringPattern(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `DestructuringPattern`. */ export function isDestructuringPattern( @@ -292,6 +472,16 @@ export function isDestructuringPattern( * Test if a node is an `EntityNameExpression`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isEntityNameExpression(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `EntityNameExpression`. */ export function isEntityNameExpression( @@ -304,6 +494,16 @@ export function isEntityNameExpression( * Test if a node is an `EntityNameOrEntityNameExpression`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isEntityNameOrEntityNameExpression(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `EntityNameOrEntityNameExpression`. */ export function isEntityNameOrEntityNameExpression( @@ -316,6 +516,16 @@ export function isEntityNameOrEntityNameExpression( * Test if a node is a `ForInOrOfStatement`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isForInOrOfStatement(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `ForInOrOfStatement`. */ export function isForInOrOfStatement( @@ -328,6 +538,16 @@ export function isForInOrOfStatement( * Test if a node is a `FunctionLikeDeclaration`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isFunctionLikeDeclaration(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `FunctionLikeDeclaration`. */ export function isFunctionLikeDeclaration( @@ -348,6 +568,16 @@ export function isFunctionLikeDeclaration( * Test if a node is a `HasDecorators`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isHasDecorators(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `HasDecorators`. */ export function isHasDecorators(node: ts.Node): node is ts.HasDecorators { @@ -366,6 +596,16 @@ export function isHasDecorators(node: ts.Node): node is ts.HasDecorators { * Test if a node is a `HasExpressionInitializer`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isHasExpressionInitializer(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `HasExpressionInitializer`. */ export function isHasExpressionInitializer( @@ -385,6 +625,16 @@ export function isHasExpressionInitializer( * Test if a node is a `HasInitializer`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isHasInitializer(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `HasInitializer`. */ export function isHasInitializer(node: ts.Node): node is ts.HasInitializer { @@ -401,6 +651,16 @@ export function isHasInitializer(node: ts.Node): node is ts.HasInitializer { * Test if a node is a `HasJSDoc`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isHasJSDoc(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `HasJSDoc`. */ export function isHasJSDoc(node: ts.Node): node is ts.HasJSDoc { @@ -475,6 +735,16 @@ export function isHasJSDoc(node: ts.Node): node is ts.HasJSDoc { * Test if a node is a `HasModifiers`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isHasModifiers(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `HasModifiers`. */ export function isHasModifiers(node: ts.Node): node is ts.HasModifiers { @@ -511,6 +781,16 @@ export function isHasModifiers(node: ts.Node): node is ts.HasModifiers { * Test if a node is a `HasType`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isHasType(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `HasType`. */ export function isHasType(node: ts.Node): node is ts.HasType { @@ -538,6 +818,16 @@ export function isHasType(node: ts.Node): node is ts.HasType { * Test if a node is a `HasTypeArguments`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isHasTypeArguments(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `HasTypeArguments`. */ export function isHasTypeArguments(node: ts.Node): node is ts.HasTypeArguments { @@ -554,6 +844,16 @@ export function isHasTypeArguments(node: ts.Node): node is ts.HasTypeArguments { * Test if a node is a `JSDocComment`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isJSDocComment(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `JSDocComment`. */ export function isJSDocComment(node: ts.Node): node is ts.JSDocComment { @@ -576,6 +876,16 @@ export function isJSDocComment(node: ts.Node): node is ts.JSDocComment { * Test if a node is a `JSDocNamespaceBody`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isJSDocNamespaceBody(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `JSDocNamespaceBody`. */ export function isJSDocNamespaceBody( @@ -588,6 +898,16 @@ export function isJSDocNamespaceBody( * Test if a node is a `JSDocTypeReferencingNode`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isJSDocTypeReferencingNode(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `JSDocTypeReferencingNode`. */ export function isJSDocTypeReferencingNode( @@ -605,6 +925,16 @@ export function isJSDocTypeReferencingNode( * Test if a node is a `JsonObjectExpression`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isJsonObjectExpression(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `JsonObjectExpression`. */ export function isJsonObjectExpression( @@ -625,6 +955,16 @@ export function isJsonObjectExpression( * Test if a node is a `JsxAttributeLike`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isJsxAttributeLike(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `JsxAttributeLike`. */ export function isJsxAttributeLike(node: ts.Node): node is ts.JsxAttributeLike { @@ -635,6 +975,16 @@ export function isJsxAttributeLike(node: ts.Node): node is ts.JsxAttributeLike { * Test if a node is a `JsxAttributeValue`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isJsxAttributeValue(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `JsxAttributeValue`. */ export function isJsxAttributeValue( @@ -653,6 +1003,16 @@ export function isJsxAttributeValue( * Test if a node is a `JsxChild`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isJsxChild(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `JsxChild`. */ export function isJsxChild(node: ts.Node): node is ts.JsxChild { @@ -669,6 +1029,16 @@ export function isJsxChild(node: ts.Node): node is ts.JsxChild { * Test if a node is a `JsxTagNameExpression`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isJsxTagNameExpression(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `JsxTagNameExpression`. */ export function isJsxTagNameExpression( @@ -685,6 +1055,16 @@ export function isJsxTagNameExpression( * Test if a node is a `LiteralToken`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isLiteralToken(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `LiteralToken`. */ export function isLiteralToken(node: ts.Node): node is ts.LiteralToken { @@ -702,6 +1082,16 @@ export function isLiteralToken(node: ts.Node): node is ts.LiteralToken { * Test if a node is a `ModuleBody`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isModuleBody(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `ModuleBody`. */ export function isModuleBody(node: ts.Node): node is ts.ModuleBody { @@ -712,6 +1102,16 @@ export function isModuleBody(node: ts.Node): node is ts.ModuleBody { * Test if a node is a `ModuleName`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isModuleName(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `ModuleName`. */ export function isModuleName(node: ts.Node): node is ts.ModuleName { @@ -722,6 +1122,16 @@ export function isModuleName(node: ts.Node): node is ts.ModuleName { * Test if a node is a `ModuleReference`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isModuleReference(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `ModuleReference`. */ export function isModuleReference(node: ts.Node): node is ts.ModuleReference { @@ -732,6 +1142,16 @@ export function isModuleReference(node: ts.Node): node is ts.ModuleReference { * Test if a node is a `NamedImportBindings`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isNamedImportBindings(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `NamedImportBindings`. */ export function isNamedImportBindings( @@ -744,6 +1164,16 @@ export function isNamedImportBindings( * Test if a node is a `NamedImportsOrExports`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isNamedImportsOrExports(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `NamedImportsOrExports`. */ export function isNamedImportsOrExports( @@ -756,6 +1186,16 @@ export function isNamedImportsOrExports( * Test if a node is a `NamespaceBody`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isNamespaceBody(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `NamespaceBody`. */ export function isNamespaceBody(node: ts.Node): node is ts.NamespaceBody { @@ -766,6 +1206,16 @@ export function isNamespaceBody(node: ts.Node): node is ts.NamespaceBody { * Test if a node is an `ObjectBindingOrAssignmentElement`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isObjectBindingOrAssignmentElement(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `ObjectBindingOrAssignmentElement`. */ export function isObjectBindingOrAssignmentElement( @@ -783,6 +1233,16 @@ export function isObjectBindingOrAssignmentElement( * Test if a node is an `ObjectBindingOrAssignmentPattern`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isObjectBindingOrAssignmentPattern(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `ObjectBindingOrAssignmentPattern`. */ export function isObjectBindingOrAssignmentPattern( @@ -795,6 +1255,16 @@ export function isObjectBindingOrAssignmentPattern( * Test if a node is an `ObjectTypeDeclaration`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isObjectTypeDeclaration(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `ObjectTypeDeclaration`. */ export function isObjectTypeDeclaration( @@ -811,6 +1281,16 @@ export function isObjectTypeDeclaration( * Test if a node is a `ParameterPropertyModifier`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isParameterPropertyModifier(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `ParameterPropertyModifier`. */ export function isParameterPropertyModifier( @@ -823,6 +1303,16 @@ export function isParameterPropertyModifier( * Test if a node is a `PropertyNameLiteral`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isPropertyNameLiteral(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `PropertyNameLiteral`. */ export function isPropertyNameLiteral( @@ -839,6 +1329,16 @@ export function isPropertyNameLiteral( * Test if a node is a `PseudoLiteralToken`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isPseudoLiteralToken(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `PseudoLiteralToken`. */ export function isPseudoLiteralToken( @@ -855,6 +1355,16 @@ export function isPseudoLiteralToken( * Test if a node is a `SignatureDeclaration`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isSignatureDeclaration(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `SignatureDeclaration`. */ export function isSignatureDeclaration( @@ -881,6 +1391,16 @@ export function isSignatureDeclaration( * Test if a node is a `SuperProperty`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isSuperProperty(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `SuperProperty`. */ export function isSuperProperty(node: ts.Node): node is ts.SuperProperty { @@ -894,6 +1414,16 @@ export function isSuperProperty(node: ts.Node): node is ts.SuperProperty { * Test if a node is a `TypeOnlyCompatibleAliasDeclaration`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isTypeOnlyCompatibleAliasDeclaration(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `TypeOnlyCompatibleAliasDeclaration`. */ export function isTypeOnlyCompatibleAliasDeclaration( @@ -911,6 +1441,16 @@ export function isTypeOnlyCompatibleAliasDeclaration( * Test if a node is a `TypeReferenceType`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isTypeReferenceType(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `TypeReferenceType`. */ export function isTypeReferenceType( @@ -923,6 +1463,16 @@ export function isTypeReferenceType( * Test if a node is an `UnionOrIntersectionTypeNode`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isUnionOrIntersectionTypeNode(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `UnionOrIntersectionTypeNode`. */ export function isUnionOrIntersectionTypeNode( @@ -935,6 +1485,16 @@ export function isUnionOrIntersectionTypeNode( * Test if a node is an `UnparsedSourceText`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isUnparsedSourceText(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be an `UnparsedSourceText`. */ export function isUnparsedSourceText( @@ -947,6 +1507,16 @@ export function isUnparsedSourceText( * Test if a node is a `VariableLikeDeclaration`. * * @category Nodes - Type Guards + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isVariableLikeDeclaration(node)) { + * // ... + * } + * ``` + * * @returns Whether the given node appears to be a `VariableLikeDeclaration`. */ export function isVariableLikeDeclaration( diff --git a/src/scopes.ts b/src/scopes.ts index 2ee6f389..794e9117 100644 --- a/src/scopes.ts +++ b/src/scopes.ts @@ -7,6 +7,15 @@ import * as ts from "typescript"; * Is the node a scope boundary, specifically due to it being a function. * * @category Scope Utilities + * + * @example + * ```ts + * declare const node: ts.Node; + * + * if (isFunctionScopeBoundary(node, ts.ObjectFlags.Anonymous)) { + * // ... + * } + * ``` */ export function isFunctionScopeBoundary(node: ts.Node): boolean { switch (node.kind) { diff --git a/src/syntax.ts b/src/syntax.ts index 36c4ecf7..99e048da 100644 --- a/src/syntax.ts +++ b/src/syntax.ts @@ -7,6 +7,13 @@ import * as ts from "typescript"; * Test of the kind given is for assignment. * * @category Syntax Utilities + * + * @example + * ```ts + * declare const kind: ts.SyntaxKind; + * + * isAssignmentKind(kind); + * ``` */ export function isAssignmentKind(kind: ts.SyntaxKind): boolean { return ( @@ -19,6 +26,12 @@ export function isAssignmentKind(kind: ts.SyntaxKind): boolean { * Test if a string is numeric. * * @category Syntax Utilities + * + * @example + * ```ts + * isNumericPropertyName("abc"); // false + * isNumericPropertyName("123"); // true + * ``` */ export function isNumericPropertyName(name: string | ts.__String): boolean { return String(+name) === name; @@ -32,6 +45,12 @@ function charSize(ch: number) { * Determines whether the given text can be used to access a property with a `PropertyAccessExpression` while preserving the property's name. * * @category Syntax Utilities + * + * @example + * ```ts + * isValidPropertyAccess("abc"); // true + * isValidPropertyAccess("123"); // false + * ``` */ export function isValidPropertyAccess( text: string, diff --git a/src/tokens.ts b/src/tokens.ts index f6d89279..2e590c1a 100644 --- a/src/tokens.ts +++ b/src/tokens.ts @@ -18,6 +18,16 @@ export type ForEachTokenCallback = (token: ts.Node) => void; * Iterates over all tokens of `node` * * @category Nodes - Other Utilities + * + * @example + * ```ts + * declare const node: ts.Node; + * + * forEachToken(node, (token) => { + * console.log("Found token:", token.getText()); + * }); + * ``` + * * @param node - The node whose tokens should be visited * @param callback - Is called for every token contained in `node` */ diff --git a/src/types/getters.ts b/src/types/getters.ts index f25b8648..a25c9d87 100644 --- a/src/types/getters.ts +++ b/src/types/getters.ts @@ -14,6 +14,13 @@ import { * Get the `CallSignatures` of the given type. * * @category Types - Getters + * + * @example + * ```ts + * declare const type: ts.Type; + * + * getCallSignaturesOfType(type); + * ``` */ export function getCallSignaturesOfType( type: ts.Type @@ -43,6 +50,14 @@ export function getCallSignaturesOfType( * Get the property with the given name on the given type (if it exists). * * @category Types - Getters + * + * @example + * ```ts + * declare const property: ts.Symbol; + * declare const type: ts.Type; + * + * getPropertyOfType(type, property.getEscapedName()); + * ``` */ export function getPropertyOfType( type: ts.Type, @@ -57,6 +72,14 @@ export function getPropertyOfType( * Retrieves a type symbol corresponding to a well-known string name. * * @category Types - Getters + * + * @example + * ```ts + * declare const type: ts.Type; + * declare const typeChecker: ts.TypeChecker; + * + * getWellKnownSymbolPropertyOfType(type, "asyncIterator", typeChecker); + * ``` */ export function getWellKnownSymbolPropertyOfType( type: ts.Type, diff --git a/src/types/typeGuards/compound.ts b/src/types/typeGuards/compound.ts index 117e2a1f..c8aee7d0 100644 --- a/src/types/typeGuards/compound.ts +++ b/src/types/typeGuards/compound.ts @@ -45,8 +45,16 @@ export interface FalseLiteralType extends BooleanLiteralType { /** * Test if a type is a {@link UnknownLiteralType}? * - * @param type - * @returns + * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isUnknownLiteralType(type)) { + * // ... + * } + * ``` */ export function isUnknownLiteralType( type: ts.Type @@ -57,7 +65,16 @@ export function isUnknownLiteralType( /** * Determines whether the given type is a boolean literal type. * - * @category Types - Utilities + * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isBooleanLiteralType(type)) { + * // ... + * } + * ``` */ export function isBooleanLiteralType( type: ts.Type @@ -68,7 +85,16 @@ export function isBooleanLiteralType( /** * Determines whether the given type is a boolean literal type for "true". * - * @category Types - Utilities + * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isTrueLiteralType(type)) { + * // ... + * } + * ``` */ export function isTrueLiteralType(type: ts.Type): type is TrueLiteralType { return ( @@ -80,7 +106,16 @@ export function isTrueLiteralType(type: ts.Type): type is TrueLiteralType { /** * Determines whether the given type is a boolean literal type for "false". * - * @category Types - Utilities + * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isFalseLiteralType(type)) { + * // ... + * } + * ``` */ export function isFalseLiteralType(type: ts.Type): type is FalseLiteralType { return ( @@ -93,6 +128,14 @@ export function isFalseLiteralType(type: ts.Type): type is FalseLiteralType { * Test if a type is a `TupleTypeReference`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isTupleTypeReference(type)) { + * // ... + * } */ export function isTupleTypeReference( type: ts.Type diff --git a/src/types/typeGuards/objects.ts b/src/types/typeGuards/objects.ts index 4a5920d4..42e4633c 100644 --- a/src/types/typeGuards/objects.ts +++ b/src/types/typeGuards/objects.ts @@ -7,6 +7,15 @@ import { isObjectType } from "./simple.js"; * Test if a type is a `EvolvingArrayType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isEvolvingArrayType(type)) { + * // ... + * } + * ``` */ export function isEvolvingArrayType( type: ts.Type @@ -20,6 +29,15 @@ export function isEvolvingArrayType( * Test if a type is a `TupleType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isTupleType(type)) { + * // ... + * } + * ``` */ export function isTupleType(type: ts.Type): type is ts.TupleType { return isObjectType(type) && isObjectFlagSet(type, ts.ObjectFlags.Tuple); @@ -29,6 +47,15 @@ export function isTupleType(type: ts.Type): type is ts.TupleType { * Test if a type is a `TypeReference`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isTypeReference(type)) { + * // ... + * } + * ``` */ export function isTypeReference(type: ts.Type): type is ts.TypeReference { return isObjectType(type) && isObjectFlagSet(type, ts.ObjectFlags.Reference); diff --git a/src/types/typeGuards/simple.ts b/src/types/typeGuards/simple.ts index a4b682b2..6e244ddb 100644 --- a/src/types/typeGuards/simple.ts +++ b/src/types/typeGuards/simple.ts @@ -6,6 +6,15 @@ import { isTypeFlagSet } from "../../flags.js"; * Test if a type is a `BigIntLiteralType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isBigIntLiteralType(type)) { + * // ... + * } + * ``` */ export function isBigIntLiteralType( type: ts.Type @@ -17,6 +26,15 @@ export function isBigIntLiteralType( * Test if a type is a `ConditionalType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isConditionalType(type)) { + * // ... + * } + * ``` */ export function isConditionalType(type: ts.Type): type is ts.ConditionalType { return isTypeFlagSet(type, ts.TypeFlags.Conditional); @@ -26,6 +44,15 @@ export function isConditionalType(type: ts.Type): type is ts.ConditionalType { * Test if a type is a `EnumType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isEnumType(type)) { + * // ... + * } + * ``` */ export function isEnumType(type: ts.Type): type is ts.EnumType { return isTypeFlagSet(type, ts.TypeFlags.Enum); @@ -35,6 +62,15 @@ export function isEnumType(type: ts.Type): type is ts.EnumType { * Test if a type is a `IndexType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isIndexType(type)) { + * // ... + * } + * ``` */ export function isIndexType(type: ts.Type): type is ts.IndexType { return isTypeFlagSet(type, ts.TypeFlags.Index); @@ -44,6 +80,15 @@ export function isIndexType(type: ts.Type): type is ts.IndexType { * Test if a type is a `IndexedAccessType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isIndexedAccessType(type)) { + * // ... + * } + * ``` */ export function isIndexedAccessType( type: ts.Type @@ -55,6 +100,15 @@ export function isIndexedAccessType( * Test if a type is a `InstantiableType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isInstantiableType(type)) { + * // ... + * } + * ``` */ export function isInstantiableType(type: ts.Type): type is ts.InstantiableType { return isTypeFlagSet(type, ts.TypeFlags.Instantiable); @@ -64,6 +118,15 @@ export function isInstantiableType(type: ts.Type): type is ts.InstantiableType { * Test if a type is a `IntersectionType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isIntersectionType(type)) { + * // ... + * } + * ``` */ export function isIntersectionType(type: ts.Type): type is ts.IntersectionType { return isTypeFlagSet(type, ts.TypeFlags.Intersection); @@ -73,6 +136,15 @@ export function isIntersectionType(type: ts.Type): type is ts.IntersectionType { * Test if a type is a `NumberLiteralType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isNumberLiteralType(type)) { + * // ... + * } + * ``` */ export function isNumberLiteralType( type: ts.Type @@ -84,6 +156,15 @@ export function isNumberLiteralType( * Test if a type is a `ObjectType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isObjectType(type)) { + * // ... + * } + * ``` */ export function isObjectType(type: ts.Type): type is ts.ObjectType { return isTypeFlagSet(type, ts.TypeFlags.Object); @@ -93,6 +174,15 @@ export function isObjectType(type: ts.Type): type is ts.ObjectType { * Test if a type is a `StringLiteralType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isStringLiteralType(type)) { + * // ... + * } + * ``` */ export function isStringLiteralType( type: ts.Type @@ -104,6 +194,15 @@ export function isStringLiteralType( * Test if a type is a `StringMappingType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isStringMappingType(type)) { + * // ... + * } + * ``` */ export function isStringMappingType( type: ts.Type @@ -115,6 +214,15 @@ export function isStringMappingType( * Test if a type is a `SubstitutionType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isSubstitutionType(type)) { + * // ... + * } + * ``` */ export function isSubstitutionType(type: ts.Type): type is ts.SubstitutionType { return isTypeFlagSet(type, ts.TypeFlags.Substitution); @@ -124,6 +232,15 @@ export function isSubstitutionType(type: ts.Type): type is ts.SubstitutionType { * Test if a type is a `TemplateLiteralType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isTemplateLiteralType(type)) { + * // ... + * } + * ``` */ export function isTemplateLiteralType( type: ts.Type @@ -135,6 +252,15 @@ export function isTemplateLiteralType( * Test if a type is a `TypeParameter`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isTypeParameter(type)) { + * // ... + * } + * ``` */ export function isTypeParameter(type: ts.Type): type is ts.TypeParameter { return isTypeFlagSet(type, ts.TypeFlags.TypeParameter); @@ -144,6 +270,15 @@ export function isTypeParameter(type: ts.Type): type is ts.TypeParameter { * Test if a type is a `TypeVariable`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isTypeVariable(type)) { + * // ... + * } + * ``` */ export function isTypeVariable(type: ts.Type): type is ts.TypeVariable { return isTypeFlagSet(type, ts.TypeFlags.TypeVariable); @@ -153,6 +288,15 @@ export function isTypeVariable(type: ts.Type): type is ts.TypeVariable { * Test if a type is a `UnionType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isUnionType(type)) { + * // ... + * } + * ``` */ export function isUnionType(type: ts.Type): type is ts.UnionType { return isTypeFlagSet(type, ts.TypeFlags.Union); @@ -162,6 +306,15 @@ export function isUnionType(type: ts.Type): type is ts.UnionType { * Test if a type is a `UnionOrIntersectionType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isUnionOrIntersectionType(type)) { + * // ... + * } + * ``` */ export function isUnionOrIntersectionType( type: ts.Type @@ -173,6 +326,15 @@ export function isUnionOrIntersectionType( * Test if a type is a `UniqueESSymbolType`. * * @category Types - Type Guards + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isUniqueESSymbolType(type)) { + * // ... + * } + * ``` */ export function isUniqueESSymbolType( type: ts.Type diff --git a/src/types/utilities.ts b/src/types/utilities.ts index b7938917..55d6e05c 100644 --- a/src/types/utilities.ts +++ b/src/types/utilities.ts @@ -28,6 +28,15 @@ import { * Determines whether a type is definitely falsy. This function doesn't unwrap union types. * * @category Types - Utilities + * + * @example + * ```ts + * declare const type: ts.Type; + * + * if (isConditionalType(type)) { + * // ... + * } + * ``` */ export function isFalsyType(type: ts.Type): boolean { if ( @@ -121,6 +130,17 @@ function isCallback( * Determines whether writing to a certain property of a given type is allowed. * * @category Types - Utilities + * + * @example + * ```ts + * declare const property: ts.Symbol; + * declare const type: ts.Type; + * declare const typeChecker: ts.TypeChecker; + * + * if (isPropertyReadonlyInType(type, property.getEscapedName(), typeChecker)) { + * // ... + * } + * ``` */ export function isPropertyReadonlyInType( type: ts.Type, @@ -157,6 +177,16 @@ export function isPropertyReadonlyInType( * Returns true for `Object.defineProperty(o, 'prop', {value, writable: false})` and `Object.defineProperty(o, 'prop', {get: () => 1})` * * @category Types - Utilities + * + * @example + * ```ts + * declare const node: ts.CallExpression; + * declare const typeChecker: ts.TypeChecker; + * + * if (isReadonlyAssignmentDeclaration(node, typeChecker)) { + * // ... + * } + * ``` */ function isReadonlyAssignmentDeclaration( node: ts.CallExpression, @@ -180,6 +210,17 @@ function isReadonlyAssignmentDeclaration( * Determines whether a type is thenable and thus can be used with `await`. * * @category Types - Utilities + * + * @example + * ```ts + * declare const node: ts.Node; + * declare const type: ts.Type; + * declare const typeChecker: ts.TypeChecker; + * + * if (isThenableType(typeChecker, node, type)) { + * // ... + * } + * ``` */ export function isThenableType( typeChecker: ts.TypeChecker, @@ -191,6 +232,27 @@ export function isThenableType( * Determines whether a type is thenable and thus can be used with `await`. * * @category Types - Utilities + * + * @example + * ```ts + * declare const expression: ts.Expression; + * declare const typeChecker: ts.TypeChecker; + * + * if (isThenableType(typeChecker, expression)) { + * // ... + * } + * ``` + * + * @example + * ```ts + * declare const expression: ts.Expression; + * declare const typeChecker: ts.TypeChecker; + * declare const type: ts.Type; + * + * if (isThenableType(typeChecker, expression, type)) { + * // ... + * } + * ``` */ export function isThenableType( typeChecker: ts.TypeChecker, @@ -222,6 +284,16 @@ export function isThenableType( * Test if the given symbol has a readonly declaration. * * @category Symbols - Utilities + * + * @example + * ```ts + * declare const symbol: ts.Symbol; + * declare const typeChecker: ts.TypeChecker; + * + * if (symbolHasReadonlyDeclaration(symbol, typeChecker)) { + * // ... + * } + * ``` */ export function symbolHasReadonlyDeclaration( symbol: ts.Symbol, @@ -250,6 +322,15 @@ export function symbolHasReadonlyDeclaration( * If the given type is not a union type, an array contain only that type will be returned. * * @category Types - Utilities + * + * @example + * ```ts + * declare const type: ts.Type; + * + * for (const typePart of unionTypeParts(type)) { + * // ... + * } + * ``` */ export function unionTypeParts(type: ts.Type): ts.Type[] { return isUnionType(type) ? type.types : [type]; diff --git a/typedoc.json b/typedoc.json index d2b8ee1d..da4bb5b2 100644 --- a/typedoc.json +++ b/typedoc.json @@ -13,6 +13,15 @@ "Other" ], "customCss": "./docs/typedoc.css", + "customValidation": { + "byKind": [ + { + "kinds": "Function", + "tags": ["example"], + "type": "code-only" + } + ] + }, "defaultCategory": "Other", "entryPoints": ["./src/index.ts"], "exclude": ["./**/test/", "./**/*.test.*"], @@ -52,6 +61,7 @@ "out": "./docs/generated", "plugin": [ "typedoc-plugin-coverage", + "typedoc-plugin-custom-validation", "typedoc-plugin-mdn-links", "typedoc-plugin-versions" ],