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>
impl<'a> ContextualTypeContext<'a>
Sourcepub fn new(interner: &'a dyn TypeDatabase) -> Self
pub fn new(interner: &'a dyn TypeDatabase) -> Self
Create a new contextual type context.
Defaults to no_implicit_any: false for compatibility.
Sourcepub fn with_expected(interner: &'a dyn TypeDatabase, expected: TypeId) -> Self
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.
Sourcepub fn with_expected_and_options(
interner: &'a dyn TypeDatabase,
expected: TypeId,
no_implicit_any: bool,
) -> Self
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.
Sourcepub const fn has_context(&self) -> bool
pub const fn has_context(&self) -> bool
Check if we have a contextual type.
Sourcepub fn get_parameter_type(&self, index: usize) -> Option<TypeId>
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 contextSourcepub fn get_parameter_type_for_call(
&self,
index: usize,
arg_count: usize,
) -> Option<TypeId>
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.
Sourcepub fn get_this_type(&self) -> Option<TypeId>
pub fn get_this_type(&self) -> Option<TypeId>
Get the contextual type for a this parameter, if present on the expected type.
Sourcepub fn get_this_type_from_marker(&self) -> Option<TypeId>
pub fn get_this_type_from_marker(&self) -> Option<TypeId>
Get the type T from a ThisType
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
}
};Sourcepub fn get_return_type(&self) -> Option<TypeId>
pub fn get_return_type(&self) -> Option<TypeId>
Get the contextual return type for a function.
Sourcepub fn get_array_element_type(&self) -> Option<TypeId>
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 numberSourcepub fn get_tuple_element_type(&self, index: usize) -> Option<TypeId>
pub fn get_tuple_element_type(&self, index: usize) -> Option<TypeId>
Get the contextual type for a specific tuple element.
Sourcepub fn get_property_type(&self, name: &str) -> Option<TypeId>
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"};Sourcepub fn for_property(&self, name: &str) -> Self
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.
Sourcepub fn for_array_element(&self) -> Self
pub fn for_array_element(&self) -> Self
Create a child context for an array element.
Sourcepub fn for_tuple_element(&self, index: usize) -> Self
pub fn for_tuple_element(&self, index: usize) -> Self
Create a child context for a tuple element at the given index.
Sourcepub fn for_parameter(&self, index: usize) -> Self
pub fn for_parameter(&self, index: usize) -> Self
Create a child context for a function parameter at the given index.
Sourcepub fn for_return(&self) -> Self
pub fn for_return(&self) -> Self
Create a child context for a function return expression.
Sourcepub fn get_generator_yield_type(&self) -> Option<TypeId>
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
}Sourcepub fn get_generator_return_type(&self) -> Option<TypeId>
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.
Sourcepub fn get_generator_next_type(&self) -> Option<TypeId>
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.