Type aliasing

From HandWiki

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..[1]

using Distance = int;

Crystal

Crystal features type aliasing using the alias keyword.[2]

alias Distance = Int32;

D

D features type aliasing using the alias keyword.[3]

alias Distance = int;

Dart

Dart features type aliasing using the typedef keyword.[4]

typedef Distance = int;

Elixir

Elixir features type aliasing using @type.[5]

@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.[6] Functionally, this creates a new, distinct type that is incompatible with its underlying type (int). 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 type keyword.[7]

type Distance = Int;

Julia

Julia features type aliasing.[8] The use of const is best practice (though not strictly required for aliasing). 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 typealias keyword.[9]

typealias Distance = Int

Nim

Nim features type aliasing.[10]

type
  Distance* = int

OCaml

OCaml features type aliasing.[11]

type distance = int

Python

Python features type aliasing.[12]

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. The use of : TypeAlias (from PEP 613) is not required for the alias to function, but it explicitly tells static type checkers (like Mypy) that the assignment is a type declaration, not a runtime variable assignment.

from typing import TypeAlias

Vector: TypeAlias = list[float]

Rust

Rust features type aliasing using the type keyword.[13]

type Point = (u8, u8);

Scala

Scala can create type aliases using opaque types.[14]

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.[15]

type Distance = number;

Zig

Zig features type aliasing by assigning a data type to a constant.[16]

const distance = u32;

References

  1. "Alias any type - C# 12.0 draft feature specifications" (in en-us). 16 August 2023. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-12.0/using-alias-types. 
  2. "alias - Crystal". https://crystal-lang.org/reference/1.11/syntax_and_semantics/alias.html. 
  3. "Alias Alias - D Programming Language". https://dlang.org/library/std/meta/alias.html. 
  4. "Typedefs" (in en). https://dart.dev/language/typedefs. 
  5. "Typespecs and behaviours" (in en). https://elixir-lang.org/getting-started/typespecs-and-behaviours.html. 
  6. "Types: Type Aliases". https://docs.hhvm.com/hack/types/type-aliases. 
  7. "Type synonym - HaskellWiki". https://wiki.haskell.org/Type_synonym. 
  8. "Types · The Julia Language". https://docs.julialang.org/en/v1/manual/types/#Type-Aliases-1. 
  9. "Type aliases | Kotlin". https://kotlinlang.org/docs/type-aliases.html. 
  10. "Nim by Example - Types". https://nim-by-example.github.io/types/. 
  11. "OCaml reference manual". https://ocaml.org/docs/basic-data-types#type-aliases. 
  12. "typing — Support for type hints". Python Software Foundation. https://docs.python.org/3/library/typing.html#type-aliases. 
  13. "Type aliases - The Rust Reference". https://doc.rust-lang.org/reference/items/type-aliases.html. 
  14. "Opaque Types". https://docs.scala-lang.org/scala3/book/types-opaque-types.html. 
  15. "Documentation - Everyday Types" (in en). https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-aliases. 
  16. "Documentation - The Zig Programming Language". https://ziglang.org/documentation/master/#Examples.