Custom type

2 snippets across 2 stacks - Python, TypeScript

PYPython

Type Aliases (3.12+)

PY · Variables & Types
Syntax
type AliasName = existing_type
Example
type UserId = int
type Coordinate = tuple[float, float]
type UserMap = dict[UserId, str]

position: Coordinate = (40.71, -74.01)

Note The type statement (Python 3.12+) replaces TypeAlias from typing. It supports lazy evaluation and generic parameters.

TSTypeScript

Type Alias Basics

TS · Type Aliases
Syntax
type Name = Type;
Example
type UserId = string;
type Coordinate = { x: number; y: number };
type StringOrNumber = string | number;
type Callback = (data: unknown) => void;

const origin: Coordinate = { x: 0, y: 0 };
const userId: UserId = "usr_9f3a";
Output
// Type aliases create a name for any type expression

Note Type aliases and interfaces overlap for object shapes, but type aliases can also name unions, tuples, primitives, and function types. Unlike interfaces, type aliases cannot be re-declared for merging. Use interfaces for object shapes that might be extended; use type aliases for everything else.

Frequently asked questions

How does Python handle custom type?
This task is covered in 2 stacks on this page: Python, TypeScript. The "Type Aliases (3.12+)" snippet in Python uses `type AliasName = existing_type`.
Which code does the Python example use?
The "Type Aliases (3.12+)" snippet uses `type AliasName = existing_type`, from the Variables & Types section of the Python cheat sheet.
Which stacks cover "custom type" on this page?
Python, TypeScript. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Type Aliases (3.12+)": The type statement (Python 3.12+) replaces TypeAlias from typing. It supports lazy evaluation and generic parameters.