HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to TypeScriptBasic TypesUnion & Literal TypesInterfacesTyped FunctionsArrays & TuplesGenerics & Enums
REFERENCE
📖 Overview
🖥️ Basics & Output
🏷️ Types
📐 Interface & Type Alias
🧩 Function
📋 Array & Object Types
🚀 Next
Generic ( <T> )enumas (type assertion)
TypeScript›REFERENCE›Next
🚀

Next

Generics, enums, and type casting.

CommandDescription
Generic ( <T> )Type-independent, reusable structure.
enumA set of named constants.
as (type assertion)Informs TS of a value's type.

Generic ( <T> )

Try it yourself →

Type-independent, reusable structure.

Syntax

function name<T>(x: T): T { }

Description

T is a 'type placeholder'; the actual type is determined when calling the function. It provides flexibility while maintaining type safety.

Example

function ilk<T>(arr: T[]): T {
  return arr[0];
}
console.log(ilk([10, 20, 30]));
console.log(ilk(["a", "b"]));

Expected output

10
a

When to use?

Helpers working with any type (first element, box, items).

enum

Try it yourself →

A set of named constants.

Syntax

enum Name { A, B, C }

Description

Gives readable names to related constant values.

Example

enum Direction { Left, Right }
const y: Direction = Direction.Left;
console.log(y);

Expected output

0

When to use?

Limited sets of options such as status, direction, role.

as (type assertion)

Try it yourself →

Informs TS of a value's type.

Syntax

value as Type

Description

If you know the type of a value, you specify it with 'as'. Use with caution — if wrong, it can cause runtime errors.

Example

const veri: unknown = "hello";
const text = veri as string;
console.log(text.toUpperCase());

Expected output

HELLO

When to use?

Narrowing external/unknown data to a known type.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code