pub enum InferencePriority {
NakedTypeVariable = 1,
HomomorphicMappedType = 2,
PartialHomomorphicMappedType = 4,
MappedType = 8,
ContravariantConditional = 16,
ReturnType = 32,
LowPriority = 64,
Circular = 128,
}Expand description
Priority levels for generic type inference constraints.
TypeScript uses a multi-pass inference algorithm where constraints are processed in priority order. Higher priority constraints (like explicit type annotations) are processed first, then lower priority constraints (like contextual types from return position) are processed in subsequent passes.
This prevents circular dependencies and any leakage in complex generic scenarios
like Array.prototype.map or Promise.then.
§Priority Order (Highest to Lowest)
NakedTypeVariable- Direct type parameter with no constraints (highest)HomomorphicMappedType- Mapped types that preserve structurePartialHomomorphicMappedType- Partially homomorphic mapped typesMappedType- Generic mapped typesContravariantConditional- Conditional types in contravariant positionReturnType- Contextual type from return position (low priority)LowPriority- Fallback inference (lowest)- Circular - Detected circular dependency (prevents infinite loops)
§Example
function map<U>(arr: T[], fn: (x: T) => U): U[];
// When calling map(x => x.toString()):
// 1. T is inferred from array element type (NakedTypeVariable)
// 2. U is inferred from return type contextual type (ReturnType)
// Processing T first prevents circular T <-> U dependencyPart of the Priority-Based Contextual Inference implementation.
Variants§
NakedTypeVariable = 1
Naked type variable with no constraints (highest priority).
Example: <T> where T appears directly in parameter types.
HomomorphicMappedType = 2
Mapped type that preserves array/tuple structure.
Example: Partial<T[]> preserves array structure.
PartialHomomorphicMappedType = 4
Partially homomorphic mapped type. Example: Mapped types with some mixed properties.
MappedType = 8
Generic mapped type.
Example: { [K in keyof T]: U }
ContravariantConditional = 16
Conditional type in contravariant position. Example: Inference from function parameter types in conditional types.
ReturnType = 32
Contextual type from return position.
Example: const x: number = fn() where fn is generic.
LowPriority = 64
Low priority fallback inference.
Circular = 128
Detected circular dependency (prevents infinite loops). Set when a type parameter depends on itself through constraints.
Implementations§
Source§impl InferencePriority
impl InferencePriority
Sourcepub const HIGHEST: Self = Self::NakedTypeVariable
pub const HIGHEST: Self = Self::NakedTypeVariable
Highest priority for explicit type annotations.
Sourcepub fn should_process_in_pass(&self, current_pass: Self) -> bool
pub fn should_process_in_pass(&self, current_pass: Self) -> bool
Check if this priority level should be processed in a given pass.
Multi-pass inference processes constraints in increasing priority order. Returns true if this priority matches or is lower than the current pass level.
Sourcepub const fn next_level(&self) -> Option<Self>
pub const fn next_level(&self) -> Option<Self>
Get the next priority level for multi-pass inference.
Trait Implementations§
Source§impl Clone for InferencePriority
impl Clone for InferencePriority
Source§fn clone(&self) -> InferencePriority
fn clone(&self) -> InferencePriority
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for InferencePriority
impl Debug for InferencePriority
Source§impl Hash for InferencePriority
impl Hash for InferencePriority
Source§impl Ord for InferencePriority
impl Ord for InferencePriority
Source§fn cmp(&self, other: &InferencePriority) -> Ordering
fn cmp(&self, other: &InferencePriority) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for InferencePriority
impl PartialEq for InferencePriority
Source§impl PartialOrd for InferencePriority
impl PartialOrd for InferencePriority
impl Copy for InferencePriority
impl Eq for InferencePriority
impl StructuralPartialEq for InferencePriority
Auto Trait Implementations§
impl Freeze for InferencePriority
impl RefUnwindSafe for InferencePriority
impl Send for InferencePriority
impl Sync for InferencePriority
impl Unpin for InferencePriority
impl UnsafeUnpin for InferencePriority
impl UnwindSafe for InferencePriority
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.