Skip to main content

CompatChecker

Struct CompatChecker 

Source
pub struct CompatChecker<'a, R: TypeResolver = NoopResolver> { /* private fields */ }
Expand description

Compatibility checker that applies TypeScript’s unsound rules before delegating to the structural subtype engine.

This layer integrates with the “Lawyer” layer to apply nuanced rules for any propagation.

Implementations§

Source§

impl<'a> CompatChecker<'a, NoopResolver>

Source

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

Create a new compatibility checker without a resolver. Note: Callers should configure strict_function_types explicitly via set_strict_function_types()

Source§

impl<'a, R: TypeResolver> CompatChecker<'a, R>

Source

pub fn with_resolver(interner: &'a dyn TypeDatabase, resolver: &'a R) -> Self

Create a new compatibility checker with a resolver. Note: Callers should configure strict_function_types explicitly via set_strict_function_types()

Source

pub fn set_query_db(&mut self, db: &'a dyn QueryDatabase)

Set the query database for Salsa-backed memoization. Propagates to the internal SubtypeChecker.

Source

pub const fn set_inheritance_graph( &mut self, graph: Option<&'a InheritanceGraph>, )

Set the inheritance graph for nominal class subtype checking. Propagates to the internal SubtypeChecker.

Source

pub fn set_strict_function_types(&mut self, strict: bool)

Configure strict function parameter checking. See https://github.com/microsoft/TypeScript/issues/18654.

Source

pub fn set_strict_null_checks(&mut self, strict: bool)

Configure strict null checks (legacy null/undefined assignability).

Source

pub fn set_no_unchecked_indexed_access(&mut self, enabled: bool)

Configure unchecked indexed access (include undefined in T[K]).

Source

pub fn set_exact_optional_property_types(&mut self, exact: bool)

Configure exact optional property types. See https://github.com/microsoft/TypeScript/issues/13195.

Source

pub fn set_strict_subtype_checking(&mut self, strict: bool)

Configure strict mode for any propagation. Configure strict subtype checking mode for lib.d.ts type checking.

When enabled, applies additional strictness rules that reject borderline cases allowed by TypeScript’s legacy behavior. This includes disabling method bivariance for soundness.

Source

pub fn apply_flags(&mut self, flags: u16)

Apply compiler options from a bitmask flags value.

The flags correspond to RelationCacheKey bits:

  • bit 0: strict_null_checks
  • bit 1: strict_function_types
  • bit 2: exact_optional_property_types
  • bit 3: no_unchecked_indexed_access
  • bit 4: disable_method_bivariance (strict_subtype_checking)
  • bit 5: allow_void_return
  • bit 6: allow_bivariant_rest
  • bit 7: allow_bivariant_param_count

This is used by QueryCache::is_assignable_to_with_flags to ensure cached results respect the compiler configuration.

Source

pub fn set_strict_any_propagation(&mut self, strict: bool)

When strict mode is enabled, any does NOT silence structural mismatches. This means the type checker will still report errors even when any is involved, if there’s a real structural mismatch.

Source

pub const fn lawyer(&self) -> &AnyPropagationRules

Get a reference to the lawyer layer for any propagation rules.

Source

pub fn lawyer_mut(&mut self) -> &mut AnyPropagationRules

Get a mutable reference to the lawyer layer for any propagation rules.

Source

pub fn apply_config(&mut self, config: &JudgeConfig)

Apply configuration from JudgeConfig.

This is used to configure the CompatChecker with settings from the CompilerOptions (passed through JudgeConfig).

Source

pub fn is_assignable(&mut self, source: TypeId, target: TypeId) -> bool

Check if source is assignable to target using TS compatibility rules.

Source

pub fn is_assignable_strict(&mut self, source: TypeId, target: TypeId) -> bool

Source

pub fn explain_failure( &mut self, source: TypeId, target: TypeId, ) -> Option<SubtypeFailureReason>

Explain why source is not assignable to target using TS compatibility rules.

Source

pub fn is_weak_union_violation(&self, source: TypeId, target: TypeId) -> bool

Source§

impl<'a, R: TypeResolver> CompatChecker<'a, R>

Source

pub fn is_assignable_with_overrides<P: AssignabilityOverrideProvider + ?Sized>( &mut self, source: TypeId, target: TypeId, overrides: &P, ) -> bool

Check if source is assignable to target using TS compatibility rules, with checker-provided overrides for enums, abstract constructors, and accessibility.

This is the main entry point for assignability checking when checker context is available.

Source

pub fn private_brand_assignability_override( &self, source: TypeId, target: TypeId, ) -> Option<bool>

Private brand assignability override. If both source and target types have private brands, they must match exactly. This implements nominal typing for classes with private fields.

Uses recursive structure to preserve Union/Intersection semantics:

  • Union (A | B): OR logic - must satisfy at least one branch
  • Intersection (A & B): AND logic - must satisfy all branches
Source

pub fn enum_assignability_override( &self, source: TypeId, target: TypeId, ) -> Option<bool>

Enum member assignability override. Implements nominal typing for enum members: EnumA.X is NOT assignable to EnumB even if values match.

TypeScript enum rules:

  1. Different enums with different DefIds are NOT assignable (nominal typing)
  2. Numeric enums are bidirectionally assignable to number (Rule #7 - Open Numeric Enums)
  3. String enums are strictly nominal (string literals NOT assignable to string enums)
  4. Same enum members with different values are NOT assignable (EnumA.X != EnumA.Y)
  5. Unions containing enums: Source union assigned to target enum checks all members
Source

pub fn are_types_identical_for_redeclaration( &mut self, a: TypeId, b: TypeId, ) -> bool

Checks if two types are compatible for variable redeclaration (TS2403).

This applies TypeScript’s nominal identity rules for enums and respects ‘any’ propagation. Used for checking if multiple variable declarations have compatible types.

§Examples
  • var x: number; var x: number → true
  • var x: E.A; var x: E.A → true
  • var x: E.A; var x: E.B → false
  • var x: E; var x: F → false (different enums)
  • var x: E; var x: number → false

Trait Implementations§

Source§

impl<'a, R: TypeResolver> AssignabilityChecker for CompatChecker<'a, R>

Source§

fn is_assignable_to(&mut self, source: TypeId, target: TypeId) -> bool

Source§

fn is_assignable_to_strict(&mut self, source: TypeId, target: TypeId) -> bool

Source§

fn is_assignable_to_bivariant_callback( &mut self, source: TypeId, target: TypeId, ) -> bool

Assignability check for bivariant callback parameters. Read more
Source§

fn evaluate_type(&mut self, type_id: TypeId) -> TypeId

Evaluate/expand a type using the checker’s resolver context. This is needed during inference constraint collection, where Application types like Func<T> must be expanded to their structural form (e.g., a Callable). The default implementation returns the type unchanged (no resolver available).

Auto Trait Implementations§

§

impl<'a, R> Freeze for CompatChecker<'a, R>

§

impl<'a, R = NoopResolver> !RefUnwindSafe for CompatChecker<'a, R>

§

impl<'a, R = NoopResolver> !Send for CompatChecker<'a, R>

§

impl<'a, R = NoopResolver> !Sync for CompatChecker<'a, R>

§

impl<'a, R> Unpin for CompatChecker<'a, R>

§

impl<'a, R> UnsafeUnpin for CompatChecker<'a, R>

§

impl<'a, R = NoopResolver> !UnwindSafe for CompatChecker<'a, R>

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