Nushell
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
  • Categories

    • Bits
    • Bytes
    • Chart
    • Conversions
    • Core
    • Database
    • Dataframe
    • Dataframe Or Lazyframe
    • Date
    • Debug
    • Default
    • Env
    • Experimental
    • Expression
    • Filesystem
    • Filters
    • Formats
    • Generators
    • Hash
    • History
    • Lazyframe
    • Math
    • Misc
    • Network
    • Path
    • Platform
    • Plugin
    • Prompt
    • Random
    • Removed
    • Shells
    • Strings
    • System
    • Viewers

type for core

Give a name to a type annotation.

Command Type

keyword

Signature

> type {flags} (name) (type)

Parameters

  • name: Name of the type alias.
  • type: Equals sign followed by a type, as written after : on a parameter.

Input/output types:

inputoutput
nothingnothing

Examples

Name a record type and use it for a parameter and an output type.

> type Point = record<x: int, y: int>
    def shift [p: Point]: nothing -> Point { {x: ($p.x + 1), y: $p.y} }
    (shift {x: 1, y: 2}).x
2

Build a type from another type alias.

> type Point = record<x: int, y: int>
    type Segment = record<from: Point, to: Point>
    def width [s: Segment]: nothing -> int { $s.to.x - $s.from.x }
    width {from: {x: 1, y: 0}, to: {x: 4, y: 2}}
3

Give a parameter of an aliased type a completer.

> type Color = string
    def paint [color: Color@[red green blue]] { $"painting it ($color)" }
    paint red
painting it red

Notes

This command is a parser keyword. The parser replaces the name with its type wherever the name is used as a type: in parameters, input/output types, variable annotations and other types. The alias is visible from its definition to the end of the block.

A completer belongs to a parameter, not to its type, so type takes no @completer. Write it on the parameter instead, as in color: Color@[red green blue].

type is reserved, so a custom command or alias can't be named type. To run an external type command, use ^type.

For details, check: https://www.nushell.sh/book/thinking_in_nu.html