Skip to main content

TsType

Enum TsType 

Source
pub enum TsType {
    Named {
        name: String,
        type_args: Vec<TsType>,
    },
    Array {
        element: Box<TsType>,
        readonly: bool,
    },
    Object(Vec<TsTypeMember>),
    Fn {
        params: Vec<TsType>,
        names: Vec<String>,
        rest: bool,
        ret: Box<TsType>,
    },
    Union {
        members: Vec<TsType>,
        multiline: bool,
    },
    Intersection(Vec<TsType>),
    Mapped {
        readonly: Option<TsMappedModifier>,
        key: String,
        keys_of: Box<TsType>,
        value: Box<TsType>,
    },
    TypeOf(Box<TsExpr>),
    Indexed {
        object: Box<TsType>,
        index: Box<TsType>,
    },
    StringLiteral(String),
    NumberLiteral(u32),
    Paren(Box<TsType>),
}
Expand description

A type-position node. Named (extended with type arguments — a real gap the reference sketch left unaddressed: Record<string, Array<{...}>>/Promise<Response> both need one, and a bare Named with no type-argument slot cannot represent either), Array (extended with a readonly modifier — P7.9’s own real gap: every List/Query element type bynk-emit’s ts_type_ref*/ts_ty families build is readonly T[], not plain T[]), Object, and Fn (P7.9’s own second real gap — the query-thunk wrapper (() => readonly T[]) and a real parametered function type (a0: T0, …) => Ret both need one) — not the sketch’s Union/Intersection/Literal/TypeParam/Readonly (still unused; readonly here is a modifier on Array, not the sketch’s own separate Readonly wrapper variant).

Variants§

§

Named

A named type, optionally generic — string/unknown (no type arguments) or Record<string, T>/Promise<Response> (some).

Fields

§name: String
§type_args: Vec<TsType>
§

Array

T[] (readonly: false) or readonly T[] (readonly: true) — TypeScript’s own postfix array-type syntax (not the equivalent Array<T>/ReadonlyArray<T> generic spelling either family uses).

The printer parenthesises element wherever a bare postfix [] would bind to the wrong thing (#1898): a readonly array (readonly T[][] is TS1354), a TsType::Fn (whose [] would bind to its return type), a TsType::Union and a TsType::Intersection (A | B[] reads as A | (B[])). So Array over any element prints correctly as built — a caller must not wrap the element itself, or it double-parenthesises. The rule is needs_parens_as_array_element in printer.rs. This closes the hazard the review of #1315/#1316 recorded here.

Fields

§element: Box<TsType>
§readonly: bool
§

Object(Vec<TsTypeMember>)

A type-position object shape, e.g. { type: string; payload: unknown } — semicolon-separated, always printed on one line (see TsExpr::Object’s own doc for the value-position contrast). An interface’s own members are each printed on their own line by TsDecl::Interface’s printer, independent of this variant’s own (always-inline) rendering — the two only look different because TsDecl::Interface is the thing choosing to put each member on its own line, not because Object has two rendering modes.

#1323 replaced the plain Vec<(String, TsType, bool)> (optional only, #1321) with TsTypeMember — workers_entry.rs’s own real gap needed both a readonly modifier (every structural type this file builds is readonly-qualified, e.g. { readonly cron: string; readonly scheduledTime: number }) and a method-signature member (ack(): void, retry(): void, waitUntil(promise: Promise< unknown>): void), neither representable by one more anonymous tuple bool — a fourth positional bool next to optional would be genuinely ambiguous at call sites (readonly vs. optional, ordered how?), the same reasoning TsObjectEntry already used over a raw tuple for the value-position sibling.

§

Fn

(a0: T0, a1: T1, …) => Ret — a function type. Parameters carry no name of their own (just their type); the printer numbers them positionally (a0, a1, …), matching the exact convention bynk-emit’s own pre-P7.9 ts_type_ref_with/ts_ty already used (TypeScript requires some name in function-type syntax, and nothing about a TypeRef::Fn/Ty::Fn parameter carries a real one to use instead). A zero-params Fn is the query-thunk wrapper shape, () => Ret. Inside a TsType::Array the printer parenthesises it (readonly ((a0: T) => U)[]); see that variant’s own doc.

names (1A.18b, #3719): optional parameter names, for a function type whose names are part of its spelling ((acc: A, x: T) => A, the list kernel’s fold). Empty is the positional a0, a1, … form above, which every other site builds; otherwise one name per entry of params, in order.

rest (1A.40c, #3996): the last parameter is a rest parameter, printed ...name: T, so the type spells a variadic function ((...bynkArgs: unknown[]) => unknown). Its name is taken from names, so a rest Fn carries names; false is every other function type.

Fields

§params: Vec<TsType>
§names: Vec<String>
§rest: bool
§

Union

A | B | C — a type-position union. Added in review of #1315: bynk-emit’s ts_ty builds a real union type for a resolved multi-actor sum (Ty::ActorSum, discriminated-union members tagged by actor name), a shape none of Named/Array/Object/Fn can represent — a real, grounded gap the same way readonly/Fn themselves were (P7.9’s own accepted proposal), not a speculative addition. Each member prints through the ordinary render_type recursion; a member that is itself a Union is legal to construct but nothing in bynk-emit builds one today. Inside a TsType::Array the printer parenthesises it ((A | B)[]); see that variant’s own doc.

multiline (#1339): false (via TsType::union) is this variant’s original single-line A | B | C form, unchanged. true (via TsType::multiline_union) is emit_sum_type’s own real discriminated-union shape — one variant per line, a leading | on every line except the first (which gets equivalent spacing instead), the closing ; appended directly to the last variant’s own line. Mirrors TsExpr::Object’s own multiline field precedent (#1317), but — unlike that one — needs no depth-aware wrapper: the pre-conversion writeln! code this reproduces always used a fixed two-space indent regardless of nesting (this shape is only ever a top-level export type alias body, never nested inside another type), so render_type stays entirely depth-unaware here too. Only rendered correctly from TsDecl::TypeAlias’s own top-level render — not reachable, and not given a defined rendering, from any nested position (an array element, a type argument, …), the same named boundary TsExpr::Object’s own multiline doc already draws for its own nested case.

Fields

§members: Vec<TsType>
§multiline: bool
§

Intersection(Vec<TsType>)

A & B — a type-position intersection. #1339’s own real gap: emit_refined_type’s own branded-type alias, {base} & { readonly __brand: "..." }, has no representation among Named/Array/Object/Fn/Union — mirrors Union’s own shape/precedent exactly (a flat Vec, each member printed through the ordinary render_type recursion, joined by &), single-line only (nothing in bynk-emit builds a multi-line intersection).

§

Mapped

A mapped type, { -readonly [K in keyof T]: V }: one property per key of keys_of, typed value (which may mention key).

readonly is the modifier on the generated properties: None leaves them as they are, Some(Add) prints +readonly and Some(Remove) prints -readonly. keys_of is the operand of keyof, so the constraint is always key in keyof keys_of. key is a name the type binds for value. Always printed on one line, as a TsType::Object is.

Fields

§keys_of: Box<TsType>
§value: Box<TsType>
§

TypeOf(Box<TsExpr>)

typeof x: the type of a value. The operand is an identifier or a chain of member accesses (typeof a.b), the only expressions TypeScript allows here; it is a use of that value for name_closure.

§

Indexed

T[K]: an indexed access type, object indexed by index (a key type such as TsType::StringLiteral or a mapped type’s own key).

Fields

§object: Box<TsType>
§index: Box<TsType>
§

StringLiteral(String)

A string literal type, "k", escaped as a TsLit::Str is.

§

NumberLiteral(u32)

A non-negative integer literal type, 0, as a tuple index (T[0]).

§

Paren(Box<TsType>)

(T): a type in parentheses, always. The printer already parenthesises where a postfix operator would misread (an array element, an indexed object); this is for a position the printer does not know about, such as the thunk type a Query annotation is written as, (() => T). It prints as the parentheses around the inner type’s own spelling, and name_closure looks through it.

Implementations§

Source§

impl TsType

Source

pub fn named(name: impl Into<String>) -> Self

A plain named type with no type arguments — string, unknown, Request, …

Source

pub fn named_with_args(name: impl Into<String>, type_args: Vec<TsType>) -> Self

A generic named type — Record<K, V>, Promise<T>, …

Source

pub fn array(element: TsType) -> Self

T[] — the non-readonly array shape.

Source

pub fn readonly_array(element: TsType) -> Self

readonly T[].

Source

pub fn union(members: Vec<TsType>) -> Self

A | B | C — the original single-line union form, unchanged.

Source

pub fn multiline_union(members: Vec<TsType>) -> Self

emit_sum_type’s own real multi-line discriminated-union shape — see TsType::Union’s own doc for the exact rendering rules and why this needs no depth parameter.

Source

pub fn intersection(members: Vec<TsType>) -> Self

A & B — an intersection type.

Source

pub fn mapped(key: impl Into<String>, keys_of: TsType, value: TsType) -> Self

{ [key in keyof keys_of]: value }, with no readonly modifier.

Source

pub fn with_mapped_readonly(self, modifier: TsMappedModifier) -> Self

The same mapped type with its properties’ readonly modifier set.

Source

pub fn type_of(operand: TsExpr) -> Self

typeof name.

Source

pub fn indexed(object: TsType, index: TsType) -> Self

object[index].

Source

pub fn paren(inner: TsType) -> Self

(inner): the type in parentheses.

Source

pub fn string_literal(text: impl Into<String>) -> Self

The string literal type "text".

Source

pub fn number_literal(n: u32) -> Self

The integer literal type n (a tuple index: Parameters<F>[0]).

Trait Implementations§

Source§

impl Clone for TsType

Source§

fn clone(&self) -> TsType

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TsType

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for TsType

Source§

fn eq(&self, other: &TsType) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for TsType

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Paint for T
where T: ?Sized,

§

fn fg(&self, value: Color) -> Painted<&T>

Returns a styled value derived from self with the foreground set to value.

This method should be used rarely. Instead, prefer to use color-specific builder methods like red() and green(), which have the same functionality but are pithier.

§Example

Set foreground color to white using fg():

use yansi::{Paint, Color};

painted.fg(Color::White);

Set foreground color to white using white().

use yansi::Paint;

painted.white();
§

fn primary(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Primary].

§Example
println!("{}", value.primary());
§

fn fixed(&self, color: u8) -> Painted<&T>

Returns self with the fg() set to [Color :: Fixed].

§Example
println!("{}", value.fixed(color));
§

fn rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the fg() set to [Color :: Rgb].

§Example
println!("{}", value.rgb(r, g, b));
§

fn black(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Black].

§Example
println!("{}", value.black());
§

fn red(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Red].

§Example
println!("{}", value.red());
§

fn green(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Green].

§Example
println!("{}", value.green());
§

fn yellow(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Yellow].

§Example
println!("{}", value.yellow());
§

fn blue(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Blue].

§Example
println!("{}", value.blue());
§

fn magenta(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Magenta].

§Example
println!("{}", value.magenta());
§

fn cyan(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Cyan].

§Example
println!("{}", value.cyan());
§

fn white(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: White].

§Example
println!("{}", value.white());
§

fn bright_black(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightBlack].

§Example
println!("{}", value.bright_black());
§

fn bright_red(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightRed].

§Example
println!("{}", value.bright_red());
§

fn bright_green(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightGreen].

§Example
println!("{}", value.bright_green());
§

fn bright_yellow(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightYellow].

§Example
println!("{}", value.bright_yellow());
§

fn bright_blue(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightBlue].

§Example
println!("{}", value.bright_blue());
§

fn bright_magenta(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightMagenta].

§Example
println!("{}", value.bright_magenta());
§

fn bright_cyan(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightCyan].

§Example
println!("{}", value.bright_cyan());
§

fn bright_white(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightWhite].

§Example
println!("{}", value.bright_white());
§

fn bg(&self, value: Color) -> Painted<&T>

Returns a styled value derived from self with the background set to value.

This method should be used rarely. Instead, prefer to use color-specific builder methods like on_red() and on_green(), which have the same functionality but are pithier.

§Example

Set background color to red using fg():

use yansi::{Paint, Color};

painted.bg(Color::Red);

Set background color to red using on_red().

use yansi::Paint;

painted.on_red();
§

fn on_primary(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Primary].

§Example
println!("{}", value.on_primary());
§

fn on_fixed(&self, color: u8) -> Painted<&T>

Returns self with the bg() set to [Color :: Fixed].

§Example
println!("{}", value.on_fixed(color));
§

fn on_rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the bg() set to [Color :: Rgb].

§Example
println!("{}", value.on_rgb(r, g, b));
§

fn on_black(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Black].

§Example
println!("{}", value.on_black());
§

fn on_red(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Red].

§Example
println!("{}", value.on_red());
§

fn on_green(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Green].

§Example
println!("{}", value.on_green());
§

fn on_yellow(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Yellow].

§Example
println!("{}", value.on_yellow());
§

fn on_blue(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Blue].

§Example
println!("{}", value.on_blue());
§

fn on_magenta(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Magenta].

§Example
println!("{}", value.on_magenta());
§

fn on_cyan(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Cyan].

§Example
println!("{}", value.on_cyan());
§

fn on_white(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: White].

§Example
println!("{}", value.on_white());
§

fn on_bright_black(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightBlack].

§Example
println!("{}", value.on_bright_black());
§

fn on_bright_red(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightRed].

§Example
println!("{}", value.on_bright_red());
§

fn on_bright_green(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightGreen].

§Example
println!("{}", value.on_bright_green());
§

fn on_bright_yellow(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightYellow].

§Example
println!("{}", value.on_bright_yellow());
§

fn on_bright_blue(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightBlue].

§Example
println!("{}", value.on_bright_blue());
§

fn on_bright_magenta(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightMagenta].

§Example
println!("{}", value.on_bright_magenta());
§

fn on_bright_cyan(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightCyan].

§Example
println!("{}", value.on_bright_cyan());
§

fn on_bright_white(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightWhite].

§Example
println!("{}", value.on_bright_white());
§

fn attr(&self, value: Attribute) -> Painted<&T>

Enables the styling [Attribute] value.

This method should be used rarely. Instead, prefer to use attribute-specific builder methods like bold() and underline(), which have the same functionality but are pithier.

§Example

Make text bold using attr():

use yansi::{Paint, Attribute};

painted.attr(Attribute::Bold);

Make text bold using using bold().

use yansi::Paint;

painted.bold();
§

fn bold(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Bold].

§Example
println!("{}", value.bold());
§

fn dim(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Dim].

§Example
println!("{}", value.dim());
§

fn italic(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Italic].

§Example
println!("{}", value.italic());
§

fn underline(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Underline].

§Example
println!("{}", value.underline());

Returns self with the attr() set to [Attribute :: Blink].

§Example
println!("{}", value.blink());

Returns self with the attr() set to [Attribute :: RapidBlink].

§Example
println!("{}", value.rapid_blink());
§

fn invert(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Invert].

§Example
println!("{}", value.invert());
§

fn conceal(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Conceal].

§Example
println!("{}", value.conceal());
§

fn strike(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Strike].

§Example
println!("{}", value.strike());
§

fn quirk(&self, value: Quirk) -> Painted<&T>

Enables the yansi [Quirk] value.

This method should be used rarely. Instead, prefer to use quirk-specific builder methods like mask() and wrap(), which have the same functionality but are pithier.

§Example

Enable wrapping using .quirk():

use yansi::{Paint, Quirk};

painted.quirk(Quirk::Wrap);

Enable wrapping using wrap().

use yansi::Paint;

painted.wrap();
§

fn mask(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Mask].

§Example
println!("{}", value.mask());
§

fn wrap(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Wrap].

§Example
println!("{}", value.wrap());
§

fn linger(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Linger].

§Example
println!("{}", value.linger());
§

fn clear(&self) -> Painted<&T>

👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear(). The clear() method will be removed in a future release.

Returns self with the quirk() set to [Quirk :: Clear].

§Example
println!("{}", value.clear());
§

fn resetting(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Resetting].

§Example
println!("{}", value.resetting());
§

fn bright(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Bright].

§Example
println!("{}", value.bright());
§

fn on_bright(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: OnBright].

§Example
println!("{}", value.on_bright());
§

fn whenever(&self, value: Condition) -> Painted<&T>

Conditionally enable styling based on whether the [Condition] value applies. Replaces any previous condition.

See the crate level docs for more details.

§Example

Enable styling painted only when both stdout and stderr are TTYs:

use yansi::{Paint, Condition};

painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);
§

fn new(self) -> Painted<Self>
where Self: Sized,

Create a new [Painted] with a default [Style]. Read more
§

fn paint<S>(&self, style: S) -> Painted<&Self>
where S: Into<Style>,

Apply a style wholesale to self. Any previous style is replaced. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.