pub enum TsType {
Base(String),
Paren(Box<TsType>),
Array(Box<TsType>),
Tuple(Vec<TsType>),
Union(Vec<TsType>),
Intersection(Vec<TsType>),
Generic(Box<TsType>, Vec<TsType>),
IndexedAccess(Box<TsType>, Box<TsType>),
}Expand description
A TypeScript type.
This type is used to represent TypeScript types in a Rust-friendly way.
Note: This is not an exhaustive representation of TypeScript types.
Variants§
Base(String)
A single type, e.g., string.
Paren(Box<TsType>)
Array(Box<TsType>)
Tuple(Vec<TsType>)
Union(Vec<TsType>)
Intersection(Vec<TsType>)
Generic(Box<TsType>, Vec<TsType>)
A generic type with arguments, e.g., Set<string, number>.
IndexedAccess(Box<TsType>, Box<TsType>)
A type with an indexing key type, e.g., Car["make"].
Implementations§
Source§impl TsType
impl TsType
Sourcepub fn is_union_with(&self, other: &Self) -> bool
pub fn is_union_with(&self, other: &Self) -> bool
Find out if this type is a union with another type
Sourcepub fn contains(&self, other: &Self) -> bool
pub fn contains(&self, other: &Self) -> bool
Find out if this type contains another type e.g. (string | number)[]
contains string
Sourcepub fn as_generic(self, args: Vec<Self>) -> Self
pub fn as_generic(self, args: Vec<Self>) -> Self
convert this type to a generic with arguments
Sourcepub fn property(self, key: Self) -> Self
pub fn property(self, key: Self) -> Self
convert this type to an indexed access type with a key
Sourcepub fn join(self, other: Self) -> Result<Self, &'static str>
pub fn join(self, other: Self) -> Result<Self, &'static str>
Attempts to join this type with a right hand type. This is used during parsing and may not be intuitive for general use.
Sourcepub fn from_ts_str(str: &str) -> Result<Self, TsTypeError>
pub fn from_ts_str(str: &str) -> Result<Self, TsTypeError>
Attempts to parse a TypeScript type from a typescript string.
Returns a TsTypeError if the string is empty or contains an unexpected character.
§Example
use ts_type::TsType;
let type1 = TsType::from_ts_str("string | number").unwrap();
let type2 = TsType::Union(vec![
TsType::Base("string".to_string()),
TsType::Base("number".to_string()),
])
assert_eq!(type1, type2);