Skip to main content

ContextualTypeContext

Struct ContextualTypeContext 

Source
pub struct ContextualTypeContext<'a> { /* private fields */ }
Expand description

Context for contextual typing. Holds the expected type and provides methods to extract type information.

Implementations§

Source§

impl<'a> ContextualTypeContext<'a>

Source

pub fn new(interner: &'a dyn TypeDatabase) -> Self

Create a new contextual type context. Defaults to no_implicit_any: false for compatibility.

Source

pub fn with_expected(interner: &'a dyn TypeDatabase, expected: TypeId) -> Self

Create a context with an expected type. Defaults to no_implicit_any: false for compatibility.

Source

pub fn with_expected_and_options( interner: &'a dyn TypeDatabase, expected: TypeId, no_implicit_any: bool, ) -> Self

Create a context with an expected type and explicit noImplicitAny setting.

Source

pub const fn expected(&self) -> Option<TypeId>

Get the expected type.

Source

pub const fn has_context(&self) -> bool

Check if we have a contextual type.

Source

pub fn get_parameter_type(&self, index: usize) -> Option<TypeId>

Get the contextual type for a function parameter at the given index.

Example:

type Handler = (e: string, i: number) => void;
const h: Handler = (x, y) => {};  // x: string, y: number from context
Source

pub fn get_parameter_type_for_call( &self, index: usize, arg_count: usize, ) -> Option<TypeId>

Get the contextual type for a call argument at the given index and arity.

Source

pub fn get_this_type(&self) -> Option<TypeId>

Get the contextual type for a this parameter, if present on the expected type.

Source

pub fn get_this_type_from_marker(&self) -> Option<TypeId>

Get the type T from a ThisType marker in the contextual type.

This is used for the Vue 2 / Options API pattern where object literal methods have their this type overridden by contextual markers.

Example:

type ObjectDescriptor<D, M> = {
    methods?: M & ThisType<D & M>;
};
const obj: ObjectDescriptor<{x: number}, {greet(): void}> = {
    methods: {
        greet() { console.log(this.x); } // this is D & M
    }
};
Source

pub fn get_return_type(&self) -> Option<TypeId>

Get the contextual return type for a function.

Source

pub fn get_array_element_type(&self) -> Option<TypeId>

Get the contextual element type for an array.

Example:

const arr: number[] = [1, 2, 3];  // elements are contextually typed as number
Source

pub fn get_tuple_element_type(&self, index: usize) -> Option<TypeId>

Get the contextual type for a specific tuple element.

Source

pub fn get_property_type(&self, name: &str) -> Option<TypeId>

Get the contextual type for an object property.

Example:

const obj: {x: number, y: string} = {x: 1, y: "hi"};
Source

pub fn for_property(&self, name: &str) -> Self

Create a child context for a nested expression. This is used when checking nested structures with contextual types.

Source

pub fn for_array_element(&self) -> Self

Create a child context for an array element.

Source

pub fn for_tuple_element(&self, index: usize) -> Self

Create a child context for a tuple element at the given index.

Source

pub fn for_parameter(&self, index: usize) -> Self

Create a child context for a function parameter at the given index.

Source

pub fn for_return(&self) -> Self

Create a child context for a function return expression.

Source

pub fn get_generator_yield_type(&self) -> Option<TypeId>

Get the contextual yield type for a generator function.

If the expected type is Generator<Y, R, N>, this returns Y. This is used to contextually type yield expressions.

Example:

function* gen(): Generator<number, void, unknown> {
    yield 1;  // 1 is contextually typed as number
}
Source

pub fn get_generator_return_type(&self) -> Option<TypeId>

Get the contextual return type for a generator function (TReturn from Generator<Y, TReturn, N>).

This is used to contextually type return statements in generators.

Source

pub fn get_generator_next_type(&self) -> Option<TypeId>

Get the contextual next type for a generator function (TNext from Generator<Y, R, TNext>).

This is used to determine the type of values passed to .next() and the type of the yield expression result.

Source

pub fn for_yield(&self) -> Self

Create a child context for a yield expression in a generator.

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

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.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more