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
interfaceOptional area ( ? )type (alias)
🧩 Function
📋 Array & Object Types
🚀 Next
TypeScript›REFERENCE›Interface & Type Alias
📐

Interface & Type Alias

Defining object shapes: interface and type.

CommandDescription
interfaceDefines the shape (fields) of an object.
Optional area ( ? )Makes a field optional.
type (alias)Gives a name to a type.

interface

Try it yourself →

Defines the shape (fields) of an object.

Syntax

interface Name { field: type; }

Description

Determines which fields and types an object must have. Objects that do not conform to this shape will throw an ERROR.

Example

interface Person {
  name: string;
  age: number;
}
const k: Person = { name: "Alex", age: 36 };
console.log(k.name);

Expected output

Alex

When to use?

Defining the shape of structured data like users, products, API responses.

Common mistakes

  • ⚠️ Leaving a required field out throws an ERROR; use area?: type for optional ones.

Optional area ( ? )

Try it yourself →

Makes a field optional.

Syntax

interface X { field?: type; }

Description

If you put a ? after the field name, that area is not required.

Example

interface Person {
  name: string;
  city?: string;
}
const k: Person = { name: "Alex" };
console.log(k.city);

Expected output

undefined

When to use?

Fields that don't always come (middle name, phone).

type (alias)

Try it yourself →

Gives a name to a type.

Syntax

type Name = type;

Description

You give a short name to a type with type — ideal for union types and complex types. Similar to interface but more flexible.

Example

type Kimlik = number | string;
let x: Kimlik = "A1";
console.log(x);

Expected output

A1

When to use?

Naming union types and repeating type patterns.

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