How to use the isTypeAliasDeclaration function from typescript

Find comprehensive JavaScript typescript.isTypeAliasDeclaration code examples handpicked from public code repositorys.

typescript.isTypeAliasDeclaration is a function that checks whether a given TypeScript node represents a Type Alias declaration.

160
161
162
163
164
165
166
167
168
169
ts.isImportDeclaration(s) ||
ts.isImportEqualsDeclaration(s) ||
ts.isExportDeclaration(s) ||
ts.isExportAssignment(s) ||
// as well as many declarations
ts.isTypeAliasDeclaration(s) ||
ts.isInterfaceDeclaration(s) ||
ts.isModuleDeclaration(s) ||
ts.isEnumDeclaration(s) ||
(isGlobal && (
fork icon116
star icon294
watch icon29

+ 3 other calls in file

How does typescript.isTypeAliasDeclaration work?

typescript.isTypeAliasDeclaration is a function provided by the TypeScript compiler API that checks whether a given node represents a Type Alias declaration in a TypeScript file. The function takes a single parameter which is the TypeScript node to be checked. Internally, the function checks if the given node has a kind property that matches the ts.SyntaxKind.TypeAliasDeclaration value. If it does, then the function returns true, indicating that the given node represents a Type Alias declaration. Otherwise, the function returns false. Type Alias declarations are used in TypeScript to define a new name for an existing type. This can be useful for simplifying complex types, creating aliases for common types, and abstracting implementation details. They are defined using the type keyword followed by the new name and the original type definition. For example, the following code defines a Type Alias called MyType that represents an object with a name property of type string and an age property of type number: typescript Copy code {{{{{{{ class="!whitespace-pre hljs language-typescript">type MyType = { name: string; age: number; }; By using typescript.isTypeAliasDeclaration, we can check whether a given node in a TypeScript file represents a Type Alias declaration and perform operations accordingly.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
import ts from "typescript";

const sourceFile = ts.createSourceFile(
  "example.ts",
  "type MyString = string;",
  ts.ScriptTarget.ESNext
);

if (ts.isTypeAliasDeclaration(sourceFile.statements[0])) {
  console.log("This is a type alias declaration");
} else {
  console.log("This is not a type alias declaration");
}

In this example, we create a TypeScript source file with a single type alias declaration. We then check whether the first statement in the source file is a type alias declaration using the typescript.isTypeAliasDeclaration function. If it is a type alias declaration, we log a message to the console.

Other functions in typescript

Sorted by popularity

function icon

typescript.SyntaxKind is the most popular function in typescript (82777 examples)