Type aliasing


Type aliasing is a feature in some programming languages that allows creating a reference to a type using another name. It does not create a new type hence does not increase type safety. It can be used to shorten a long name. Languages allowing type aliasing include: C++, C# Crystal, D, Dart, Elixir, Elm, F#, Go, Hack, Haskell, Julia, Kotlin, Nim, OCaml, Python, Rust, Scala, Swift and TypeScript.

Example

C++

C++ features type aliasing with the using keyword.

using Distance = int;

C#

C# version 12 and higher supports type aliasing with the using keyword. Earlier versions restrict its use to file-local scope or specific import contexts..
using Distance = int;

Crystal

Crystal features type aliasing using the alias keyword.

alias Distance = Int32;

D

D features type aliasing using the alias keyword.

alias Distance = int;

Dart

Dart features type aliasing using the typedef keyword.

typedef Distance = int;

Elixir

Elixir features type aliasing using @type.

@type Distance :: integer

Elm

Elm features type aliasing using type alias.

type alias Distance = Int

F#

F3 features type aliasing using the type keyword.

type Distance = int

Go

Go features type aliasing using the type keyword and =.

type Distance = int

Hack

Hack features type aliasing using the newtype keyword. Functionally, this creates a new, distinct type that is incompatible with its underlying type. This is stricter than a simple alias, which is generally transparent and interchangeable with the original type.
newtype Distance = int;

Haskell

Haskell features type aliasing using the keyword.

type Distance = Int;

Julia

Julia features type aliasing. The use of is best practice. It prevents the alias from being rebound to a different type later in the program, ensuring the alias is stable.

const Distance = Int

Kotlin

Kotlin features type aliasing using the keyword.

typealias Distance = Int

Nim

Nim features type aliasing.

type
Distance* = int

OCaml

OCaml features type aliasing.

type distance = int

Python

Python features type aliasing.

Vector = list

Type aliases may be marked with TypeAlias to make it explicit that the statement is a type alias declaration, not a normal variable assignment. The use of is not required for the alias to function, but it explicitly tells static type checkers that the assignment is a type declaration, not a runtime variable assignment.

from typing import TypeAlias
Vector: TypeAlias = list

Rust

Rust features type aliasing using the keyword.

type Point = ;

Scala

Scala can create type aliases using opaque types.

object Logarithms:
opaque type Logarithm = Double

Swift

Swift features type aliasing using the keyword.

typealias Distance = Int;

TypeScript

TypeScript features type aliasing using the keyword.

type Distance = number;

Zig

Zig features type aliasing by assigning a data type to a constant.

const distance = u32;