Type aliasing

{{Portal|Computer programming}}

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 using the using keyword.

using Distance = int;

= C# =

C# since version 12 features type aliasing using the using keyword.{{cite web |title=Alias any type - C# 12.0 draft feature specifications |url=https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-12.0/using-alias-types |website=learn.microsoft.com |access-date=23 February 2024 |language=en-us |date=16 August 2023}}

using Distance = int;

= Crystal =

Crystal features type aliasing using the alias keyword.{{cite web |title=alias - Crystal |url=https://crystal-lang.org/reference/1.11/syntax_and_semantics/alias.html |website=crystal-lang.org |access-date=21 February 2024}}

alias Distance = Int32;

= D =

D features type aliasing using the alias keyword.{{cite web |title=Alias Alias - D Programming Language |url=https://dlang.org/library/std/meta/alias.html |website=dlang.org |access-date=18 June 2023}}

alias Distance = int;

= Dart =

Dart features type aliasing using the typedef keyword.{{cite web |title=Typedefs |url=https://dart.dev/language/typedefs |website=dart.dev |access-date=18 June 2023 |language=en}}

typedef Distance = int;

= Elixir =

Elixir features type aliasing using @type.{{cite web |title=Typespecs and behaviours |url=https://elixir-lang.org/getting-started/typespecs-and-behaviours.html |website=elixir-lang.github.com |access-date=23 June 2023 |language=en}}

@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.{{cite web |title=Types: Type Aliases |url=https://docs.hhvm.com/hack/types/type-aliases |website=docs.hhvm.com |access-date=18 June 2023}}

newtype Distance = int;

= Haskell =

Haskell features type aliasing using the type keyword.{{cite web |title=Type synonym - HaskellWiki |url=https://wiki.haskell.org/Type_synonym |website=wiki.haskell.org |access-date=18 June 2023}}

type Distance = Int;

= Julia =

Julia features type aliasing.{{cite web |title=Types · The Julia Language |url=https://docs.julialang.org/en/v1/manual/types/#Type-Aliases-1 |website=docs.julialang.org |access-date=23 June 2023}}

const Distance = Int

= Kotlin =

Kotlin features type aliasing using the typealias keyword.{{cite web |title=Type aliases {{!}} Kotlin |url=https://kotlinlang.org/docs/type-aliases.html |website=Kotlin Help |access-date=18 June 2023}}

typealias Distance = Int

= Nim =

Nim features type aliasing.{{cite web |title=Nim by Example - Types |url=https://nim-by-example.github.io/types/ |website=nim-by-example.github.io |access-date=21 June 2023}}

type

Distance* = int

= OCaml =

OCaml features type aliasing.{{cite web |title=OCaml reference manual |url=https://ocaml.org/docs/basic-data-types#type-aliases |access-date=23 April 2024 |website=ocaml.org}}

type distance = int

= Python =

Python features type aliasing.{{cite web |title=typing — Support for type hints |url=https://docs.python.org/3/library/typing.html#type-aliases |website=Python documentation |publisher=Python Software Foundation |access-date=18 June 2023}}

Vector = list[float]

Type aliases may be marked with TypeAlias to make it explicit that the statement is a type alias declaration, not a normal variable assignment.

from typing import TypeAlias

Vector: TypeAlias = list[float]

= Rust =

Rust features type aliasing using the type keyword.{{cite web |title=Type aliases - The Rust Reference |url=https://doc.rust-lang.org/reference/items/type-aliases.html |website=doc.rust-lang.org |access-date=18 June 2023}}

type Point = (u8, u8);

= Scala =

Scala can create type aliases using opaque types.{{cite web |title=Opaque Types |url=https://docs.scala-lang.org/scala3/book/types-opaque-types.html |website=Scala Documentation |access-date=18 June 2023}}

object Logarithms:

opaque type Logarithm = Double

= Swift =

Swift features type aliasing using the typealias keyword.

typealias Distance = Int;

= TypeScript =

TypeScript features type aliasing using the type keyword.{{cite web |title=Documentation - Everyday Types |url=https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-aliases |website=www.typescriptlang.org |access-date=18 June 2023 |language=en}}

type Distance = number;

= Zig =

Zig features type aliasing by assigning a data type to a constant.{{cite web |title=Documentation - The Zig Programming Language |url=https://ziglang.org/documentation/master/#Examples |website=ziglang.org |access-date=14 October 2024}}

const distance = u32;

References