type for core
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:
| input | output |
|---|---|
| nothing | nothing |
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
2Build 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}}
3Give 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 redNotes
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