Skip to main content

submilli_engine/
types.rs

1use std::collections::{BTreeMap, BTreeSet};
2use std::fmt;
3
4use crate::mangle::MangledName;
5use serde::{Deserialize, Serialize};
6
7/// One member of an enum, as the member literal type `E.A` names it.
8#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
9pub struct EnumMember<V> {
10    pub name: String,
11    pub value: V,
12    /// How many members the enum has, so a union naming them all folds into
13    /// the enum, as TypeScript reduces `E.A | E.B` to `E`.
14    pub member_count: usize,
15}
16
17impl<V> EnumMember<V> {
18    pub fn new(name: &str, value: V, member_count: usize) -> Self {
19        EnumMember {
20            name: name.to_string(),
21            value,
22            member_count,
23        }
24    }
25}
26
27/// The value an enum member holds.
28#[derive(Clone, Debug, PartialEq)]
29pub enum EnumValue {
30    Number(LiteralF64),
31    String(String),
32}
33
34impl EnumValue {
35    /// The plain literal type of this value: `1` or `"a"`.
36    pub fn literal_type(&self) -> Type {
37        match self {
38            EnumValue::Number(value) => Type::NumberLiteral(*value),
39            EnumValue::String(value) => Type::StringLiteral(value.clone()),
40        }
41    }
42}
43
44/// `f64` wrapper for total ordering and bit-pattern equality — bare `f64` lacks `Eq`/`Ord`,
45/// which would break the derived impls on [`Type`]. Construction must canonicalize `-0.0 → 0.0`;
46/// NaN can't appear from source literals so bit-pattern equality is safe.
47#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
48pub struct LiteralF64(#[serde(with = "crate::artifact_f64")] pub f64);
49
50impl PartialEq for LiteralF64 {
51    fn eq(&self, other: &Self) -> bool {
52        self.0.to_bits() == other.0.to_bits()
53    }
54}
55
56impl Eq for LiteralF64 {}
57
58impl PartialOrd for LiteralF64 {
59    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
60        Some(self.cmp(other))
61    }
62}
63
64impl Ord for LiteralF64 {
65    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
66        self.0.total_cmp(&other.0)
67    }
68}
69
70impl std::hash::Hash for LiteralF64 {
71    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
72        self.0.to_bits().hash(state);
73    }
74}
75
76/// Owning package of a by-name [`Type`] reference (`InterfaceRef`, `AliasRef`, `Alias`,
77/// `NumberEnum`, `StringEnum`). Carried so *structural* resolution of an already-typed
78/// value can find the symbol by FQN in a global registry, independent of what the current
79/// module imported.
80///
81/// **Excluded from identity:** `Eq`/`Ord`/`Hash` treat every `Package` as equal, so the
82/// owning package never drives a by-name type's identity. Identity is the sibling
83/// `mangled` field (the declaring symbol's mangled name, which already encodes the
84/// package and module); `package` only keeps *structural* registry lookup robust to
85/// which construction site stamped it.
86#[derive(Clone, Debug, Serialize, Deserialize)]
87pub struct Package(pub String);
88
89impl Package {
90    pub fn prelude() -> Self {
91        Self(crate::mangle::PRELUDE_PACKAGE.to_string())
92    }
93
94    pub fn user() -> Self {
95        Self(crate::mangle::USER_PACKAGE.to_string())
96    }
97
98    pub fn as_str(&self) -> &str {
99        &self.0
100    }
101}
102
103impl PartialEq for Package {
104    fn eq(&self, _: &Self) -> bool {
105        true
106    }
107}
108
109impl Eq for Package {}
110
111impl PartialOrd for Package {
112    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
113        Some(self.cmp(other))
114    }
115}
116
117impl Ord for Package {
118    fn cmp(&self, _: &Self) -> std::cmp::Ordering {
119        std::cmp::Ordering::Equal
120    }
121}
122
123impl std::hash::Hash for Package {
124    fn hash<H: std::hash::Hasher>(&self, _: &mut H) {}
125}
126
127/// `optional: true` means reads widen to `ty | undefined` and construction may omit the field.
128/// Distinct from a value-nullable field (`ty: T | null, optional: false`): must be present
129/// but can be null.
130#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
131pub struct ObjectField {
132    pub ty: Type,
133    pub optional: bool,
134    /// `readonly` forbids writes through the field; assignability treats a writable
135    /// target field invariantly (see `assignable.rs`). Inferred object literals and
136    /// freshly-synthesized shapes are writable (`false`); only an explicit `readonly`
137    /// modifier on an object-type/interface property sets this.
138    pub readonly: bool,
139    /// A method rather than a function-typed property. Both compare their
140    /// parameters contravariantly, though `tsc` compares a method's both ways.
141    #[serde(default)]
142    pub method: bool,
143}
144
145impl ObjectField {
146    pub fn required(ty: Type) -> Self {
147        Self {
148            ty,
149            optional: false,
150            readonly: false,
151            method: false,
152        }
153    }
154    pub fn optional(ty: Type) -> Self {
155        Self {
156            ty,
157            optional: true,
158            readonly: false,
159            method: false,
160        }
161    }
162
163    /// The type a *read* of this field yields: an optional field widens to
164    /// `T | undefined`, since an absent property reads as undefined. Every field
165    /// read — object type, interface property, class field, union member, and
166    /// codegen's mirror of all four — goes through this, so the widening rule
167    /// has one definition.
168    pub fn read_ty(&self) -> Type {
169        Self::widen_optional(self.optional, self.ty.clone())
170    }
171
172    /// [`read_ty`](Self::read_ty) for callers holding the two facts separately
173    /// (an interface `PropertySig`, a class `FieldSig`).
174    pub fn widen_optional(optional: bool, ty: Type) -> Type {
175        if optional {
176            Type::union(vec![ty, Type::Undefined])
177        } else {
178            ty
179        }
180    }
181}
182
183/// Values available under arbitrary string property names.
184#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
185pub struct IndexSignature {
186    pub value: Box<Type>,
187    pub readonly: bool,
188}
189
190impl IndexSignature {
191    pub fn map_value(&self, transform: impl FnOnce(&Type) -> Type) -> Self {
192        Self {
193            value: Box::new(transform(&self.value)),
194            readonly: self.readonly,
195        }
196    }
197
198    /// [`map_value`](Self::map_value) with a transform that can fail.
199    pub fn try_map_value<E>(
200        &self,
201        transform: impl FnOnce(&Type) -> Result<Type, E>,
202    ) -> Result<Self, E> {
203        Ok(Self {
204            value: Box::new(transform(&self.value)?),
205            readonly: self.readonly,
206        })
207    }
208
209    pub fn read_ty(&self) -> Type {
210        Type::union(vec![(*self.value).clone(), Type::Undefined])
211    }
212}
213
214#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
215pub struct TypePredicate {
216    pub parameter_index: u32,
217    pub asserted_type: Type,
218}
219
220/// Tuple positions and the number of trailing positions that may be omitted.
221/// Optional positions include `Undefined` in their element type.
222#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
223pub struct TupleType {
224    pub elements: Vec<Type>,
225    pub optional: usize,
226}
227
228impl TupleType {
229    pub fn required_len(&self) -> usize {
230        self.elements.len().saturating_sub(self.optional)
231    }
232
233    pub fn map(&self, transform: impl FnMut(&Type) -> Type) -> Self {
234        Self {
235            elements: self.elements.iter().map(transform).collect(),
236            optional: self.optional,
237        }
238    }
239
240    pub fn try_map<E>(&self, transform: impl FnMut(&Type) -> Result<Type, E>) -> Result<Self, E> {
241        Ok(Self {
242            elements: self
243                .elements
244                .iter()
245                .map(transform)
246                .collect::<Result<_, _>>()?,
247            optional: self.optional,
248        })
249    }
250}
251
252impl From<Vec<Type>> for TupleType {
253    fn from(elements: Vec<Type>) -> Self {
254        Self {
255            elements,
256            optional: 0,
257        }
258    }
259}
260
261impl std::ops::Deref for TupleType {
262    type Target = [Type];
263    fn deref(&self) -> &Self::Target {
264        &self.elements
265    }
266}
267
268impl IntoIterator for TupleType {
269    type Item = Type;
270    type IntoIter = std::vec::IntoIter<Type>;
271    fn into_iter(self) -> Self::IntoIter {
272        self.elements.into_iter()
273    }
274}
275
276impl<'a> IntoIterator for &'a TupleType {
277    type Item = &'a Type;
278    type IntoIter = std::slice::Iter<'a, Type>;
279    fn into_iter(self) -> Self::IntoIter {
280        self.elements.iter()
281    }
282}
283
284#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
285pub enum Type {
286    Number,
287    /// No distinct runtime representation — codegen widens to `f64` at every emission site.
288    NumberLiteral(LiteralF64),
289    BigInt,
290    /// A bigint literal type: `123n`, `-1n`. Holds the value in decimal, with a
291    /// leading `-` when negative, so each value has one spelling. Like the other
292    /// literal types it lowers exactly as its base, `bigint`.
293    BigIntLiteral(String),
294    String,
295    StringLiteral(String),
296    Uint8Array,
297    Boolean,
298    /// `true` or `false`. Like the other literal types it has no runtime
299    /// representation of its own: it lowers exactly as `boolean`. `Type::union`
300    /// folds `true | false` into `boolean`, which is what `boolean` means.
301    BooleanLiteral(bool),
302    Null,
303    /// The single undefined value, distinct from language null and Wasm null.
304    Undefined,
305    Void,
306    /// Unlike TypeScript's `any`, requires explicit narrowing before use.
307    /// `Unknown | T` collapses to `Unknown`.
308    Unknown,
309    Function {
310        params: Vec<Type>,
311        /// Number of trailing fixed parameters callers may omit.
312        optional: usize,
313        ret: Box<Type>,
314        /// Boxed to break the `Type → Function → TypePredicate → Type` cycle. Guards are
315        /// assignable to plain functions but not vice versa.
316        predicate: Option<Box<TypePredicate>>,
317        /// When true, the last `params` entry holds the rest array type.
318        has_rest: bool,
319    },
320    /// `BTreeMap` gives structural `==`, deterministic iteration, and canonical field order for codegen.
321    Object {
322        fields: BTreeMap<String, ObjectField>,
323        index: Option<IndexSignature>,
324    },
325    Array(Box<Type>),
326    /// Parser rejects empty tuples. Index access requires an integer literal; out-of-range and
327    /// non-literal indices are rejected at typecheck time. Lowers to `(ref $Array)` at runtime.
328    Tuple(TupleType),
329    /// `readonly T[]` (also spelled `ReadonlyArray<T>`) or `readonly [A, B]`. The inner
330    /// type is always a [`Type::Array`] or [`Type::Tuple`]: the wrapper only forbids
331    /// writes, so [`Type::peel`] strips it and every read path sees the plain array.
332    /// Write paths (index assignment, mutating methods) must ask
333    /// [`Type::is_readonly_array`] before peeling. `Eq`/`Ord` keep it distinct from the
334    /// mutable type because a readonly array is not assignable to a mutable one.
335    Readonly(Box<Type>),
336    /// Diagnostic already reported; downstream code must not emit cascading errors.
337    Error,
338    /// No Wasm representation — never values never reach codegen.
339    Never,
340    /// Signature-form generic parameter. Never appears in function bodies — body inference
341    /// replaces each TypeVar with a fresh [`GenericParam`](Type::GenericParam) at body entry.
342    TypeVar(String),
343    /// Body-internal generic placeholder. Identity by `id` — two GPs with different ids are
344    /// distinct even when they share a name, preventing collisions across nested generic scopes.
345    GenericParam {
346        id: u32,
347        name: String,
348    },
349    /// A generic value with a proven runtime shape. Member access and lowering
350    /// use `ty`; assignability also preserves the original generic identity.
351    Refined {
352        original: Box<Type>,
353        ty: Box<Type>,
354    },
355    /// `args` carries already-substituted type arguments; empty for non-generic interfaces.
356    /// `mangled` is the declaring symbol's mangled name — the **nominal identity** (two
357    /// modules' same-named interfaces have distinct mangled names). `package`/`name` are
358    /// kept for diagnostics and import-independent *structural* resolution (see [`Package`]);
359    /// they do not drive identity. `mangled` is listed first so derived `Ord` keys on it.
360    InterfaceRef {
361        mangled: MangledName,
362        package: Package,
363        name: String,
364        args: Vec<Type>,
365    },
366    /// Nominal reference to a `class` declaration. Identity is `mangled` (two modules'
367    /// same-named classes are distinct); `package`/`name` are for diagnostics and
368    /// import-independent structural resolution, mirroring [`Type::InterfaceRef`].
369    /// `args` instantiates the class's type parameters; empty for a
370    /// non-generic class.
371    ClassRef {
372        mangled: MangledName,
373        package: Package,
374        name: String,
375        args: Vec<Type>,
376    },
377    /// Distinct from `StringEnum` so codegen knows the `i32` representation without consulting the type namespace.
378    ///
379    /// `member` narrows the enum to one member's literal type, `E.A`, which is
380    /// what a member read has, as in TypeScript. It shares the enum's runtime
381    /// representation; [`Type::widen_literal`] drops it back to the enum.
382    NumberEnum {
383        mangled: MangledName,
384        package: Package,
385        name: String,
386        member: Option<EnumMember<LiteralF64>>,
387    },
388    StringEnum {
389        mangled: MangledName,
390        package: Package,
391        name: String,
392        member: Option<EnumMember<String>>,
393    },
394    /// Canonical union: sorted, deduplicated, no nested unions, no `Error` members, always ≥2
395    /// members. Build *only* via [`Type::union`].
396    Union(Vec<Type>),
397    /// Every pattern-match site must call [`Type::peel`] first; sole exception is
398    /// [`Display`](fmt::Display). `Eq`/`Ord`/`Hash` are nominal: `Alias { "ID", Number }` ≠
399    /// `Number` at the raw level. `args` carries already-substituted type arguments.
400    Alias {
401        mangled: MangledName,
402        package: Package,
403        name: String,
404        args: Vec<Type>,
405        ty: Box<Type>,
406    },
407    /// Lazy by-name reference to a type alias, used **only at a
408    /// recursion back-edge** — the inner `Json` in
409    /// `type Json = number | string | Json[]`, or the `Node` in
410    /// `type Node = { next: Node | null }`. Unlike [`Type::Alias`] it
411    /// carries no inline body; the body lives in the type namespace and
412    /// is resolved by name on demand (mirroring [`Type::InterfaceRef`]).
413    /// This keeps a recursive alias a *finite* `Type` value — a cyclic
414    /// inline body would infinitely recurse the derived `Eq`/`Ord` and
415    /// [`Type::peel`]. `peel` does **not** expand it (it stops here, the
416    /// same as `InterfaceRef`); the few sites that need the alias's
417    /// structure resolve it by name with cycle-safety.
418    AliasRef {
419        mangled: MangledName,
420        package: Package,
421        name: String,
422        args: Vec<Type>,
423    },
424    /// `object`: any value that isn't a primitive, `null` or `undefined`, as in
425    /// TypeScript. Every object, array, function and class instance is one. It
426    /// has `{}`'s members and, like `{}`, lowers as `unknown`.
427    NonPrimitive,
428}
429
430impl Type {
431    pub fn tuple(elements: Vec<Type>) -> Self {
432        Self::Tuple(elements.into())
433    }
434
435    /// A receiver whose computed string keys use the object property carrier.
436    pub fn is_structural_object(&self) -> bool {
437        match self.peel() {
438            Self::Object { .. } | Self::InterfaceRef { .. } => true,
439            Self::Union(members) => members.iter().all(Self::is_structural_object),
440            _ => false,
441        }
442    }
443
444    /// Build an [`Type::InterfaceRef`]. `mangled` is the declaring symbol's mangled
445    /// name (its nominal identity) — pass the symbol's own `mangled_name`, never a
446    /// value recomputed from `package`/`name`, so two construction sites for the same
447    /// type always agree.
448    pub fn interface_ref(
449        package: Package,
450        name: impl Into<String>,
451        mangled: MangledName,
452        args: Vec<Type>,
453    ) -> Type {
454        Type::InterfaceRef {
455            mangled,
456            package,
457            name: name.into(),
458            args,
459        }
460    }
461
462    /// Build a [`Type::ClassRef`]. `mangled` is the class symbol's nominal identity;
463    /// pass the symbol's own `mangled_name`, never one recomputed from `package`/`name`.
464    pub fn class_ref(
465        package: Package,
466        name: impl Into<String>,
467        mangled: MangledName,
468        args: Vec<Type>,
469    ) -> Type {
470        Type::ClassRef {
471            mangled,
472            package,
473            name: name.into(),
474            args,
475        }
476    }
477
478    pub fn number_enum(package: Package, name: impl Into<String>, mangled: MangledName) -> Type {
479        Type::NumberEnum {
480            mangled,
481            package,
482            name: name.into(),
483            member: None,
484        }
485    }
486
487    pub fn string_enum(package: Package, name: impl Into<String>, mangled: MangledName) -> Type {
488        Type::StringEnum {
489            mangled,
490            package,
491            name: name.into(),
492            member: None,
493        }
494    }
495
496    /// The member literal type `E.A` of the enum type `self`, which has
497    /// `member_count` members. `self` must be a `NumberEnum` holding a number
498    /// or a `StringEnum` holding a string. As in TypeScript, the one member of
499    /// an enum has the enum's own type.
500    pub fn with_enum_member(
501        &self,
502        member_name: &str,
503        value: EnumValue,
504        member_count: usize,
505    ) -> Type {
506        if member_count < 2 {
507            return self.without_enum_member();
508        }
509        match (self.clone(), value) {
510            (
511                Type::NumberEnum {
512                    mangled,
513                    package,
514                    name,
515                    ..
516                },
517                EnumValue::Number(value),
518            ) => Type::NumberEnum {
519                mangled,
520                package,
521                name,
522                member: Some(EnumMember::new(member_name, value, member_count)),
523            },
524            (
525                Type::StringEnum {
526                    mangled,
527                    package,
528                    name,
529                    ..
530                },
531                EnumValue::String(value),
532            ) => Type::StringEnum {
533                mangled,
534                package,
535                name,
536                member: Some(EnumMember::new(member_name, value, member_count)),
537            },
538            (ty, _) => ty,
539        }
540    }
541
542    /// The enum a member literal type `E.A` belongs to; any other type unchanged.
543    pub fn without_enum_member(&self) -> Type {
544        match self {
545            Type::NumberEnum {
546                mangled,
547                package,
548                name,
549                member: Some(_),
550            } => Type::number_enum(package.clone(), name.clone(), mangled.clone()),
551            Type::StringEnum {
552                mangled,
553                package,
554                name,
555                member: Some(_),
556            } => Type::string_enum(package.clone(), name.clone(), mangled.clone()),
557            _ => self.clone(),
558        }
559    }
560
561    /// Whether this is one literal type, what TypeScript calls a unit type:
562    /// `1`, `"a"`, `true`, `1n` or an enum member `E.A`.
563    pub fn is_literal_type(&self) -> bool {
564        matches!(
565            self,
566            Type::NumberLiteral(_)
567                | Type::StringLiteral(_)
568                | Type::BooleanLiteral(_)
569                | Type::BigIntLiteral(_)
570        ) || self.is_enum_member()
571    }
572
573    /// Whether this is an enum member literal type `E.A`.
574    pub fn is_enum_member(&self) -> bool {
575        self.enum_member_name().is_some()
576    }
577
578    /// The enum, member name and member count of an enum member literal type `E.A`.
579    pub fn enum_member_name(&self) -> Option<(&MangledName, &str, usize)> {
580        match self {
581            Type::NumberEnum {
582                mangled,
583                member: Some(member),
584                ..
585            } => Some((mangled, &member.name, member.member_count)),
586            Type::StringEnum {
587                mangled,
588                member: Some(member),
589                ..
590            } => Some((mangled, &member.name, member.member_count)),
591            _ => None,
592        }
593    }
594
595    /// The value an enum member literal type `E.A` holds.
596    pub fn enum_member_value(&self) -> Option<EnumValue> {
597        match self.peel() {
598            Type::NumberEnum {
599                member: Some(member),
600                ..
601            } => Some(EnumValue::Number(member.value)),
602            Type::StringEnum {
603                member: Some(member),
604                ..
605            } => Some(EnumValue::String(member.value.clone())),
606            _ => None,
607        }
608    }
609
610    pub fn alias_ref(
611        package: Package,
612        name: impl Into<String>,
613        mangled: MangledName,
614        args: Vec<Type>,
615    ) -> Type {
616        Type::AliasRef {
617            mangled,
618            package,
619            name: name.into(),
620            args,
621        }
622    }
623
624    pub fn alias_ty(
625        package: Package,
626        name: impl Into<String>,
627        mangled: MangledName,
628        args: Vec<Type>,
629        ty: Box<Type>,
630    ) -> Type {
631        Type::Alias {
632            mangled,
633            package,
634            name: name.into(),
635            args,
636            ty,
637        }
638    }
639
640    /// Convenience for a non-namespaced prelude interface (`Error`, `Response`, …):
641    /// the prelude registers these with `mangled_name = prelude(name)`, so this is
642    /// the matching identity. Namespaced prelude types (`Temporal.*`) must instead
643    /// use [`Type::interface_ref`] with `extend(prelude("Temporal"), local)`.
644    pub fn prelude_interface(name: impl Into<String>, args: Vec<Type>) -> Type {
645        let name = name.into();
646        let mangled = crate::mangle::prelude(&name);
647        Type::interface_ref(Package::prelude(), name, mangled, args)
648    }
649
650    /// The built-in `Error` class reference — the type of `throw`/`catch` values
651    /// and the root of user error subclasses.
652    pub fn prelude_error_class() -> Type {
653        Type::class_ref(
654            Package::prelude(),
655            "Error",
656            crate::mangle::prelude("Error"),
657            Vec::new(),
658        )
659    }
660
661    /// The built-in `RangeError` class reference — the host-implemented
662    /// `Error` subclass for out-of-range failures.
663    pub fn prelude_range_error_class() -> Type {
664        Type::class_ref(
665            Package::prelude(),
666            "RangeError",
667            crate::mangle::prelude("RangeError"),
668            Vec::new(),
669        )
670    }
671
672    /// Remove display aliases while retaining generic guard identity.
673    pub fn without_aliases(&self) -> &Type {
674        match self {
675            Type::Alias { ty, .. } => ty.without_aliases(),
676            _ => self,
677        }
678    }
679
680    /// Whether this holds every value: `unknown`, or a union of `{}`, `null`
681    /// and `undefined` (and possibly more). Through aliases only: a refinement
682    /// like `T & unknown` holds only `T`'s values.
683    pub fn holds_every_value(&self) -> bool {
684        let members: &[Type] = match self.without_aliases() {
685            Type::Unknown => return true,
686            Type::Union(members) => members,
687            _ => return false,
688        };
689        // Through aliases only: `T & undefined` holds `undefined` only when `T`
690        // does.
691        members.iter().any(Type::is_empty_object)
692            && members
693                .iter()
694                .any(|m| matches!(m.without_aliases(), Type::Null))
695            && members
696                .iter()
697                .any(|m| matches!(m.without_aliases(), Type::Undefined))
698    }
699
700    /// The pieces of a refinement whose shape is a union: `T & ({} | null)`
701    /// is `T & {}` and `null`. `None` for any other type.
702    pub fn refinement_pieces(&self) -> Option<Vec<Type>> {
703        let Type::Refined { original, ty } = self.without_aliases() else {
704            return None;
705        };
706        let Type::Union(parts) = ty.peel() else {
707            return None;
708        };
709        Some(
710            parts
711                .iter()
712                .map(|part| match part.peel() {
713                    Type::Null | Type::Undefined | Type::Void => part.clone(),
714                    _ => Type::Refined {
715                        original: original.clone(),
716                        ty: Box::new(part.clone()),
717                    },
718                })
719                .collect(),
720        )
721    }
722
723    /// Whether this is a narrowed type parameter, like `T & {}`.
724    pub fn refines_type_parameter(&self) -> bool {
725        matches!(self.without_aliases(), Type::Refined { original, .. }
726            if matches!(original.peel(), Type::GenericParam { .. }))
727    }
728
729    pub fn peel(&self) -> &Type {
730        let mut t = self;
731        while let Type::Alias { ty, .. } | Type::Refined { ty, .. } | Type::Readonly(ty) = t {
732            t = ty;
733        }
734        t
735    }
736
737    /// `{ [key: string]: unknown }`: any object, interface or class instance,
738    /// for host declarations that read a value as an object. Not `{}`, which
739    /// also holds primitives that have no object representation.
740    pub fn any_object() -> Type {
741        Type::Object {
742            index: Some(IndexSignature {
743                value: Box::new(Type::Unknown),
744                readonly: false,
745            }),
746            fields: BTreeMap::new(),
747        }
748    }
749
750    /// `{}`, the object type with no fields.
751    pub fn empty_object() -> Type {
752        Type::Object {
753            fields: BTreeMap::new(),
754            index: None,
755        }
756    }
757
758    /// Whether this is `{}`: the object type with no fields and no index
759    /// signature. As in TypeScript, it holds every value but `null` and
760    /// `undefined`, primitives included, so codegen lowers it as `unknown`.
761    /// A refinement such as `T & {}` is not `{}`: it holds only `T`'s values.
762    pub fn is_empty_object(&self) -> bool {
763        let mut t = self;
764        while let Type::Alias { ty, .. } | Type::Readonly(ty) = t {
765            t = ty;
766        }
767        matches!(t, Type::Object { fields, index: None } if fields.is_empty())
768    }
769
770    /// [`peel`](Self::peel), with `{}` and `object` read as `unknown`: the type whose
771    /// representation and run-time operations a value of this type uses.
772    /// Codegen matches on this rather than on `peel`, so a `{}` holding a
773    /// number is never treated as an object struct. `peel` reads a refinement
774    /// as its shape, so `T & {}` lowers as `unknown`, like `T` itself.
775    pub fn lowered(&self) -> &Type {
776        let peeled = self.peel();
777        if peeled.is_empty_object() || matches!(peeled, Type::NonPrimitive) {
778            return &Type::Unknown;
779        }
780        peeled
781    }
782
783    /// [`peel`](Self::peel), but stopping at a [`Type::Readonly`] wrapper, for
784    /// sites that carry a type onward and must not drop its readonly-ness.
785    pub fn peel_preserving_readonly(&self) -> &Type {
786        let mut t = self;
787        while let Type::Alias { ty, .. } | Type::Refined { ty, .. } = t {
788            t = ty;
789        }
790        t
791    }
792
793    /// Whether writes through a value of this type are forbidden because it is a
794    /// `readonly` array or tuple, looking through aliases and refinements.
795    pub fn is_readonly_array(&self) -> bool {
796        matches!(self.peel_preserving_readonly(), Type::Readonly(_))
797    }
798
799    /// The element type of a rest parameter's array: `T` for `T[]` or
800    /// `readonly T[]`. Not peeled through aliases, since rest lowering matches the
801    /// parameter type as written.
802    pub fn rest_element(&self) -> Option<&Type> {
803        match self {
804            Type::Array(element) => Some(element),
805            Type::Readonly(inner) => match inner.as_ref() {
806                Type::Array(element) => Some(element),
807                _ => None,
808            },
809            _ => None,
810        }
811    }
812
813    /// A rest parameter's type with any `readonly` removed. Each call packs a
814    /// fresh array for the rest, so whether the callee may write to it is the
815    /// callee's own concern: two function types relate on their rest elements,
816    /// as in tsc.
817    pub fn rest_array_ignoring_readonly(&self) -> &Type {
818        match self {
819            Type::Readonly(inner) if matches!(inner.as_ref(), Type::Array(_)) => inner,
820            _ => self,
821        }
822    }
823
824    /// Whether this is the type of an array that holds no element at any
825    /// depth: `never[]`, `never[][]`, as an empty literal is typed.
826    pub fn holds_no_element(&self) -> bool {
827        match self.peel() {
828            Type::Array(element) => {
829                matches!(element.peel(), Type::Never) || element.holds_no_element()
830            }
831            _ => false,
832        }
833    }
834
835    /// Whether this is a union of only arrays and tuples, which share the `$Array`
836    /// representation.
837    pub fn is_array_like_union(&self) -> bool {
838        match self.peel() {
839            Type::Union(members) => members
840                .iter()
841                .all(|member| matches!(member.peel(), Type::Array(_) | Type::Tuple(_))),
842            _ => false,
843        }
844    }
845
846    /// The element of a union of arrays and tuples read as one array: any
847    /// member's element. `None` for any other type. Only reads may go through
848    /// it; writing a member's element through the joined type could store
849    /// another member's element type.
850    pub fn array_like_union_element(&self) -> Option<Type> {
851        let Type::Union(members) = self.peel() else {
852            return None;
853        };
854        let elements = members
855            .iter()
856            .map(|member| match member.peel() {
857                Type::Array(element) => Some((**element).clone()),
858                Type::Tuple(positions) => Some(Type::union(positions.elements.clone())),
859                _ => None,
860            })
861            .collect::<Option<Vec<_>>>()?;
862        Some(Type::union(elements))
863    }
864
865    /// The arrays and tuples of a union of strings with arrays or tuples, and
866    /// nothing else, as their own union. Such a union has no shared
867    /// representation, so each use tests `typeof` and takes the string's or the
868    /// array's path.
869    pub fn string_or_array_union_arrays(&self) -> Option<Type> {
870        let Type::Union(members) = self.peel() else {
871            return None;
872        };
873        let (strings, arrays): (Vec<Type>, Vec<Type>) = members
874            .iter()
875            .cloned()
876            .partition(|member| member.peel().is_string_shaped());
877        let all_arrays = arrays
878            .iter()
879            .all(|member| matches!(member.peel(), Type::Array(_) | Type::Tuple(_)));
880        (!strings.is_empty() && !arrays.is_empty() && all_arrays).then(|| Type::union(arrays))
881    }
882
883    /// The array a union of arrays and tuples reads as: see
884    /// [`Self::array_like_union_element`].
885    pub fn array_like_union_view(&self) -> Option<Type> {
886        self.array_like_union_element()
887            .map(|element| Type::Array(Box::new(element)))
888    }
889
890    /// Whether this type is `void`, through any depth of alias.
891    ///
892    /// `void` is the one type with no value slot at all, so a gate that tests
893    /// for it decides between "one Wasm result" and "none". Every such gate —
894    /// in the typechecker *and* in codegen — must answer the same way for
895    /// `type V = void` as for `void`: a bare `matches!(ty, Type::Void)` hands an
896    /// aliased `void` a value slot that
897    /// [`SymbolTable::value_type`](crate::codegen::symbol_table::SymbolTable::value_type)
898    /// then refuses to lower. A typecheck gate must not start peeling ahead of
899    /// its codegen counterpart, which turns a clean diagnostic into a panic.
900    pub fn is_void(&self) -> bool {
901        matches!(self.peel(), Type::Void)
902    }
903
904    /// Whether a function taking `actual` parameters can stand where one taking
905    /// `expected` is called. As in TypeScript, it may declare fewer and ignore
906    /// the rest of the arguments; a closure adapter drops them at runtime. Rest
907    /// functions keep an exact arity, since their packed array has a slot of its
908    /// own.
909    pub fn function_arity_fits(actual: usize, expected: usize, has_rest: bool) -> bool {
910        actual == expected || (!has_rest && actual < expected)
911    }
912
913    /// Whether a value of this type would need a `void` slot at runtime:
914    /// `void` itself, or a union that lists it.
915    ///
916    /// Deliberately one level deep — it does **not** descend into a function's
917    /// return type, where `void` is legitimate (`() => void`). Every gate that
918    /// refuses `void` in a value or comparison position wants this, not the
919    /// bare [`is_void`](Self::is_void). `cond ? f() : 1` and `a ?? f()` are
920    /// plain `void`, so no expression builds the union form; a gate that asks
921    /// this still refuses one that did, rather than letting it reach codegen.
922    pub fn carries_void(&self) -> bool {
923        match self.peel() {
924            Type::Void => true,
925            Type::Union(members) => members.iter().any(|m| matches!(m.peel(), Type::Void)),
926            _ => false,
927        }
928    }
929
930    /// Primitive operations shared by enums and homogeneous literal unions,
931    /// without widening their type identity or changing assignability.
932    pub fn primitive_behavior(&self) -> &Type {
933        match self.peel() {
934            Type::NumberEnum { .. } => &Type::Number,
935            Type::StringEnum { .. } => &Type::String,
936            Type::BooleanLiteral(_) => &Type::Boolean,
937            Type::BigIntLiteral(_) => &Type::BigInt,
938            Type::Union(members)
939                if !members.is_empty()
940                    && members.iter().all(|member| {
941                        matches!(
942                            member.primitive_behavior(),
943                            Type::Number | Type::NumberLiteral(_)
944                        )
945                    }) =>
946            {
947                &Type::Number
948            }
949            Type::Union(members)
950                if !members.is_empty()
951                    && members
952                        .iter()
953                        .all(|member| matches!(member.primitive_behavior(), Type::BigInt)) =>
954            {
955                &Type::BigInt
956            }
957            Type::Union(members)
958                if !members.is_empty() && members.iter().all(Type::is_string_shaped) =>
959            {
960                &Type::String
961            }
962            ty => ty,
963        }
964    }
965
966    /// Whether every value of this type is a bigint: `bigint`, a bigint literal
967    /// type, or a union or alias of those.
968    pub fn is_bigint(&self) -> bool {
969        matches!(self.primitive_behavior(), Type::BigInt)
970    }
971
972    /// This type with literal types replaced by the primitive they are a literal of.
973    ///
974    /// A literal type is only sound where the value cannot change, so inference keeps
975    /// it at a `const` binding and widens here at every position that is mutable or
976    /// whose type is inferred from its contents — a `let` binding, an array element,
977    /// an object-literal property, a generic argument. `const a = 1` is `1`, but
978    /// `let b = a` is `number`, matching TypeScript.
979    ///
980    /// An enum member literal type `E.A` widens to its enum `E`, not to the
981    /// enum's primitive: the enum is a nominal type whose identity its members
982    /// are checked against. Use [`primitive_behavior`](Self::primitive_behavior)
983    /// for the primitive.
984    pub fn widen_literal(&self) -> Type {
985        match self {
986            Type::NumberLiteral(_) => Type::Number,
987            Type::StringLiteral(_) => Type::String,
988            Type::BooleanLiteral(_) => Type::Boolean,
989            Type::BigIntLiteral(_) => Type::BigInt,
990            _ if self.is_enum_member() => self.without_enum_member(),
991            // A union widens memberwise, which also collapses it when the members
992            // share a base: `1 | 2` is `number`, not `number | number`, because
993            // `Type::union` deduplicates.
994            Type::Union(members) => Type::union(without_members_of_empty_object(
995                members.iter().map(Type::widen_literal).collect(),
996            )),
997            _ => self.clone(),
998        }
999    }
1000
1001    /// The primitive this literal type, or union of literals of one
1002    /// primitive, is a literal of: `number` for `1 | 2`, `None` for
1003    /// `1 | "a"` or a type that is no literal.
1004    pub fn literal_base(&self) -> Option<Type> {
1005        let widened = self.widen_literal();
1006        (widened != *self && matches!(widened, Type::Number | Type::String | Type::Boolean))
1007            .then_some(widened)
1008    }
1009
1010    /// Whether every part of this type is a string — a `string`, a
1011    /// string-literal type, or a union of those.
1012    ///
1013    /// The typechecker, the import collector, and codegen must agree on this
1014    /// exactly: a shape accepted by one and not the others emits a `$string`
1015    /// into an `f64` slot, which fails Wasm validation rather than type
1016    /// checking. Contrast [`contains_string`](Self::contains_string), which
1017    /// asks the `any` question.
1018    pub fn is_string_shaped(&self) -> bool {
1019        match self.peel() {
1020            Type::String | Type::StringLiteral(_) | Type::StringEnum { .. } => true,
1021            Type::Union(members) => members.iter().all(Type::is_string_shaped),
1022            _ => false,
1023        }
1024    }
1025
1026    /// Whether *some* part of this type is a string. Diagnostics-only: it is
1027    /// what decides whether a rejected operand still deserves the "convert
1028    /// first" help, so `string | null` gets it.
1029    pub fn contains_string(&self) -> bool {
1030        match self.peel() {
1031            Type::String | Type::StringLiteral(_) | Type::StringEnum { .. } => true,
1032            Type::Union(members) => members.iter().any(Type::contains_string),
1033            _ => false,
1034        }
1035    }
1036
1037    /// Whether any member, through unions, satisfies `leaf`, which sees peeled
1038    /// types. Syntactic: an alias reference is a leaf, not expanded; the
1039    /// typechecker's `any_resolved_member` follows alias bodies through its
1040    /// resolver.
1041    pub fn any_member(&self, leaf: &dyn Fn(&Type) -> bool) -> bool {
1042        match self.peel() {
1043            Type::Union(members) => members.iter().any(|member| member.any_member(leaf)),
1044            member => leaf(member),
1045        }
1046    }
1047
1048    /// Whether every member, through unions, satisfies `leaf`. Syntactic,
1049    /// like [`Type::any_member`].
1050    pub fn all_members(&self, leaf: &dyn Fn(&Type) -> bool) -> bool {
1051        !self.any_member(&|member| !leaf(member))
1052    }
1053
1054    /// Whether `null` is spelled among the members. Syntactic, unlike the
1055    /// typechecker's `type_admits_null`: `unknown` doesn't count.
1056    pub fn spells_null(&self) -> bool {
1057        self.any_member(&|member| matches!(member, Type::Null))
1058    }
1059
1060    /// Whether `undefined`, or an erased `void` result, is spelled among the
1061    /// members. `unknown` and type parameters don't count.
1062    pub fn spells_undefined(&self) -> bool {
1063        self.any_member(&|member| matches!(member, Type::Undefined | Type::Void))
1064    }
1065
1066    /// Whether a value of the member type `leaf` has no JSON text at the
1067    /// document root, so `JSON.stringify` returns `undefined` for it.
1068    /// Objects and arrays holding such values still produce a document.
1069    pub fn stringifies_to_undefined(leaf: &Type) -> bool {
1070        // `{}` may hold a function, as `unknown` may.
1071        matches!(
1072            leaf.lowered(),
1073            Type::Undefined
1074                | Type::Void
1075                | Type::Unknown
1076                | Type::GenericParam { .. }
1077                | Type::TypeVar(_)
1078                | Type::Function { .. }
1079        )
1080    }
1081
1082    /// Normalizes to a flat member list that is unique *by peeled type*.
1083    ///
1084    /// Both halves peel because a nominal key is unsound here: an alias whose
1085    /// body is a union is a nested union in disguise, and every downstream
1086    /// per-member probe (`value_type`'s nullability test, narrowing's member
1087    /// walk) reads it as one opaque member. Deduping nominally leaves
1088    /// `N | number` as a two-member union that no operator is defined on.
1089    ///
1090    /// The alias label a union displays therefore survives only where it is
1091    /// still a single member: an alias of a union is replaced by its members,
1092    /// and among duplicates the alias-labelled spelling wins regardless of
1093    /// source order, so `N | number` and `number | N` both keep `N`.
1094    ///
1095    /// `peel` stops at a *recursive* alias (it is a name, not a body), so a
1096    /// recursive alias never dedups against its own expansion. Both spellings
1097    /// lower the same way, so this costs a redundant member, not correctness.
1098    pub fn union(members: Vec<Type>) -> Type {
1099        if members.iter().any(|m| matches!(m.peel(), Type::Error)) {
1100            return Type::Error;
1101        }
1102        // `unknown | T` → `unknown`; peels aliases so `type Dyn = unknown` still collapses.
1103        if members.iter().any(|m| matches!(m.peel(), Type::Unknown)) {
1104            return Type::Unknown;
1105        }
1106        // `T | never` → `T`. After Error/Unknown checks so cascading-silence wins.
1107        let members: Vec<Type> = members
1108            .into_iter()
1109            .filter(|m| !matches!(m.peel(), Type::Never))
1110            .collect();
1111        let mut flat: Vec<Type> = Vec::new();
1112        for m in members {
1113            match m.without_aliases() {
1114                Type::Union(inner) => flat.extend(inner.iter().cloned()),
1115                _ => flat.push(m),
1116            }
1117        }
1118        // Sorting by the peeled key groups alias-labelled duplicates next to
1119        // their body; aliases rank 0 so they sort first within the group, and
1120        // `dedup_by` keeps the earlier element — the name the user wrote.
1121        let alias_rank = |t: &Type| u8::from(!matches!(t, Type::Alias { .. }));
1122        flat.sort_by(|a, b| {
1123            a.without_aliases()
1124                .cmp(b.without_aliases())
1125                .then_with(|| alias_rank(a).cmp(&alias_rank(b)))
1126        });
1127        flat.dedup_by(|a, b| a.without_aliases() == b.without_aliases());
1128        fold_boolean_literals(&mut flat);
1129        fold_enum_members(&mut flat);
1130        // `T | (T & {})` holds `T`'s values: the refinement adds nothing.
1131        let refines_a_member = |member: &Type| {
1132            matches!(member, Type::Refined { original, .. }
1133                if flat.iter().any(|other| other.without_aliases() == original.without_aliases()))
1134        };
1135        let kept: Vec<Type> = flat
1136            .iter()
1137            .filter(|member| !refines_a_member(member))
1138            .cloned()
1139            .collect();
1140        flat = kept;
1141        // `object | {}` holds `{}`'s values: `object` adds nothing.
1142        if flat.iter().any(Type::is_empty_object) {
1143            flat.retain(|member| !matches!(member.peel(), Type::NonPrimitive));
1144        }
1145        if flat.len() > 1 {
1146            return Type::Union(flat);
1147        }
1148        match flat.into_iter().next() {
1149            Some(member) => member,
1150            None => Type::Never,
1151        }
1152    }
1153
1154    /// For method dispatch. Erased generics route to `Object`. Aliases peel. Returns `None`
1155    /// for function, void, null, and error. The first tuple element is the owning package
1156    /// for diagnostics; the mangled name is the structural lookup key. Built-in
1157    /// interfaces (`Number`, `Array`, `Object`, …) live in the prelude package.
1158    pub fn interface_routing(&self) -> Option<(MangledName, &str, &str, Vec<Type>)> {
1159        let prelude = crate::mangle::PRELUDE_PACKAGE;
1160        match self.primitive_behavior() {
1161            // A literal type routes to its base's interface: `"abc".at(0)` resolves the
1162            // same members a `string` receiver does. The prelude `Number`/`String`
1163            // interfaces expose no mutating members, so routing a literal there cannot
1164            // invalidate it.
1165            Type::Number | Type::NumberLiteral(_) => Some((
1166                crate::mangle::prelude("Number"),
1167                prelude,
1168                "Number",
1169                Vec::new(),
1170            )),
1171            Type::BigInt => Some((
1172                crate::mangle::prelude("BigInt"),
1173                prelude,
1174                "BigInt",
1175                Vec::new(),
1176            )),
1177            Type::Boolean => Some((
1178                crate::mangle::prelude("Boolean"),
1179                prelude,
1180                "Boolean",
1181                Vec::new(),
1182            )),
1183            Type::String | Type::StringLiteral(_) => Some((
1184                crate::mangle::prelude("String"),
1185                prelude,
1186                "String",
1187                Vec::new(),
1188            )),
1189            Type::Uint8Array => Some((
1190                crate::mangle::prelude("Uint8Array"),
1191                prelude,
1192                "Uint8Array",
1193                Vec::new(),
1194            )),
1195            Type::Array(elem) => Some((
1196                crate::mangle::prelude("Array"),
1197                prelude,
1198                "Array",
1199                vec![(**elem).clone()],
1200            )),
1201            // Tuples are arrays at runtime; route to `Array` with the positions'
1202            // union as the element type so reads (`length`, `map`, `at`, …) flow
1203            // through. Mutating methods are rejected separately at the call site.
1204            Type::Tuple(elements) => Some((
1205                crate::mangle::prelude("Array"),
1206                prelude,
1207                "Array",
1208                vec![Type::union(elements.elements.clone())],
1209            )),
1210            Type::Object { .. }
1211            | Type::NonPrimitive
1212            | Type::TypeVar(_)
1213            | Type::GenericParam { .. } => Some((
1214                crate::mangle::prelude("Object"),
1215                prelude,
1216                "Object",
1217                Vec::new(),
1218            )),
1219            Type::InterfaceRef {
1220                mangled,
1221                package,
1222                name,
1223                args,
1224                ..
1225            }
1226            | Type::ClassRef {
1227                mangled,
1228                package,
1229                name,
1230                args,
1231                ..
1232            } => Some((
1233                mangled.clone(),
1234                package.as_str(),
1235                name.as_str(),
1236                args.clone(),
1237            )),
1238            // Enum values route to Object (not Number/String) to keep the API surface minimal.
1239            Type::NumberEnum { .. } | Type::StringEnum { .. } => Some((
1240                crate::mangle::prelude("Object"),
1241                prelude,
1242                "Object",
1243                Vec::new(),
1244            )),
1245            // `unknown` routes to Object so vtable methods work.
1246            Type::Unknown => Some((
1247                crate::mangle::prelude("Object"),
1248                prelude,
1249                "Object",
1250                Vec::new(),
1251            )),
1252            _ => None,
1253        }
1254    }
1255}
1256
1257/// `ty` as written beside a `?` marker, which already admits `undefined`: without
1258/// its `undefined` member. A type that is only `undefined` stays as it is.
1259/// Every renderer of optional parameters shows them through this, except
1260/// `type_rendering`'s `Frame::Optional`, which applies the same rule to
1261/// borrowed members.
1262pub(crate) fn shown_beside_optional_marker(ty: &Type) -> Type {
1263    match ty.peel() {
1264        Type::Union(members) => Type::union(
1265            members
1266                .iter()
1267                .filter(|member| !matches!(member.peel(), Type::Undefined))
1268                .cloned()
1269                .collect(),
1270        ),
1271        _ => ty.clone(),
1272    }
1273}
1274
1275/// `s` escaped for a double-quoted string the way `tsc` prints a string literal
1276/// type (`escapeString` in TypeScript's `utilities.ts`): `"G\"HI"`, `"a\nb"`.
1277pub(crate) fn escape_string_literal(s: &str) -> String {
1278    let mut out = String::with_capacity(s.len());
1279    let mut chars = crate::literal_units::literal_chars(s).peekable();
1280    while let Some(c) = chars.next() {
1281        let c = match c {
1282            Ok(c) => c,
1283            Err(lone) => {
1284                out.push_str(&format!("\\u{lone:04X}"));
1285                continue;
1286            }
1287        };
1288        match c {
1289            '"' => out.push_str("\\\""),
1290            '\\' => out.push_str("\\\\"),
1291            '\n' => out.push_str("\\n"),
1292            '\r' => out.push_str("\\r"),
1293            '\t' => out.push_str("\\t"),
1294            '\u{8}' => out.push_str("\\b"),
1295            '\u{b}' => out.push_str("\\v"),
1296            '\u{c}' => out.push_str("\\f"),
1297            // `\0` before a digit would read as an octal escape.
1298            '\0' if chars
1299                .peek()
1300                .is_some_and(|next| next.is_ok_and(|d| d.is_ascii_digit())) =>
1301            {
1302                out.push_str("\\x00");
1303            }
1304            '\0' => out.push_str("\\0"),
1305            '\u{0}'..='\u{1f}' | '\u{85}' | '\u{2028}' | '\u{2029}' => {
1306                out.push_str(&format!("\\u{:04X}", u32::from(c)));
1307            }
1308            c => out.push(c),
1309        }
1310    }
1311    out
1312}
1313
1314/// The [`Type::BigIntLiteral`] of a bigint literal's decimal digits, as the
1315/// lexer writes them.
1316pub fn bigint_literal_type(digits: &str) -> Type {
1317    Type::BigIntLiteral(canonical_bigint_digits(digits))
1318}
1319
1320/// `digits` negated, in [`Type::BigIntLiteral`]'s spelling: `-0n` is `0n`.
1321pub fn negate_bigint_digits(digits: &str) -> String {
1322    let canonical = canonical_bigint_digits(digits);
1323    if canonical == "0" {
1324        return canonical;
1325    }
1326    match canonical.strip_prefix('-') {
1327        Some(magnitude) => magnitude.to_string(),
1328        None => format!("-{canonical}"),
1329    }
1330}
1331
1332/// `digits` without leading zeros, keeping a sign, and `0` for zero.
1333fn canonical_bigint_digits(digits: &str) -> String {
1334    let (sign, magnitude) = match digits.strip_prefix('-') {
1335        Some(magnitude) => ("-", magnitude),
1336        None => ("", digits),
1337    };
1338    match magnitude.trim_start_matches('0') {
1339        "" => "0".to_string(),
1340        trimmed => format!("{sign}{trimmed}"),
1341    }
1342}
1343
1344/// Drops the members a `{}` among `members` already holds: every one that
1345/// cannot be `null` or `undefined` and is no bare type parameter. TypeScript
1346/// reduces a union this way when it widens a literal, joins flow types and
1347/// types `a ?? b`.
1348pub fn without_members_of_empty_object(members: Vec<Type>) -> Vec<Type> {
1349    if !members.iter().any(Type::is_empty_object) {
1350        return members;
1351    }
1352    members
1353        .into_iter()
1354        .filter(|member| member.is_empty_object() || may_hold_values_outside_empty_object(member))
1355        .collect()
1356}
1357
1358/// Whether `ty` may hold a value `{}` does not: `null` or `undefined` (as
1359/// `T & ({} | undefined)` may), or any value of a type parameter.
1360fn may_hold_values_outside_empty_object(ty: &Type) -> bool {
1361    match ty.peel() {
1362        Type::Null
1363        | Type::Undefined
1364        | Type::Void
1365        | Type::Unknown
1366        | Type::GenericParam { .. }
1367        | Type::TypeVar(_) => true,
1368        Type::Union(members) => members.iter().any(may_hold_values_outside_empty_object),
1369        _ => false,
1370    }
1371}
1372
1373/// `boolean` is `true | false`: a union holding both literals, or `boolean`
1374/// and either literal, holds exactly `boolean`. `members` is sorted and
1375/// deduplicated, and stays so.
1376fn fold_boolean_literals(members: &mut Vec<Type>) {
1377    let is_boolean = |m: &Type| matches!(m.without_aliases(), Type::Boolean);
1378    let is_literal = |m: &Type| matches!(m.without_aliases(), Type::BooleanLiteral(_));
1379    let literals = members.iter().filter(|m| is_literal(m)).count();
1380    let has_boolean = members.iter().any(is_boolean);
1381    if literals == 0 || (literals == 1 && !has_boolean) {
1382        return;
1383    }
1384    members.retain(|m| !is_literal(m));
1385    if !has_boolean {
1386        let at = members.partition_point(|m| m.without_aliases() < &Type::Boolean);
1387        members.insert(at, Type::Boolean);
1388    }
1389}
1390
1391/// Reduces enum types as TypeScript does: a member beside its enum is absorbed
1392/// by it, members naming every member of their enum are the enum, and an enum
1393/// or member beside its primitive (`E | number`) is absorbed by the primitive.
1394/// Leaves `members` sorted and deduplicated.
1395fn fold_enum_members(members: &mut Vec<Type>) {
1396    absorb_enums_into_primitives(members);
1397    let complete = complete_enums(members);
1398    if complete.is_empty() {
1399        return;
1400    }
1401    for member in members.iter_mut() {
1402        if let Type::NumberEnum { mangled, .. } | Type::StringEnum { mangled, .. } =
1403            member.without_aliases()
1404            && complete.contains(mangled)
1405        {
1406            *member = member.without_aliases().without_enum_member();
1407        }
1408    }
1409    members.sort_by(|a, b| a.without_aliases().cmp(b.without_aliases()));
1410    members.dedup_by(|a, b| a.without_aliases() == b.without_aliases());
1411}
1412
1413/// Drops every enum type and member beside the primitive it holds.
1414fn absorb_enums_into_primitives(members: &mut Vec<Type>) {
1415    let has = |base: &Type| members.iter().any(|m| m.without_aliases() == base);
1416    let (has_number, has_string) = (has(&Type::Number), has(&Type::String));
1417    members.retain(|m| match m.without_aliases() {
1418        Type::NumberEnum { .. } => !has_number,
1419        Type::StringEnum { .. } => !has_string,
1420        _ => true,
1421    });
1422}
1423
1424/// The enums `members` holds whole: as the enum itself, or by naming every
1425/// one of its members.
1426fn complete_enums(members: &[Type]) -> BTreeSet<MangledName> {
1427    struct NamedMembers<'a> {
1428        member_count: usize,
1429        names: BTreeSet<&'a str>,
1430    }
1431    let mut named: BTreeMap<&MangledName, NamedMembers> = BTreeMap::new();
1432    let mut whole: BTreeSet<&MangledName> = BTreeSet::new();
1433    for member in members {
1434        let ty = member.without_aliases();
1435        if let Some((mangled, name, member_count)) = ty.enum_member_name() {
1436            named
1437                .entry(mangled)
1438                .or_insert_with(|| NamedMembers {
1439                    member_count,
1440                    names: BTreeSet::new(),
1441                })
1442                .names
1443                .insert(name);
1444        } else if let Type::NumberEnum { mangled, .. } | Type::StringEnum { mangled, .. } = ty {
1445            whole.insert(mangled);
1446        }
1447    }
1448    named
1449        .into_iter()
1450        .filter(|(mangled, named)| {
1451            whole.contains(mangled) || named.names.len() >= named.member_count
1452        })
1453        .map(|(mangled, _)| mangled.clone())
1454        .collect()
1455}
1456
1457impl Type {
1458    pub fn render_checked(
1459        &self,
1460        limits: crate::rendering::RenderLimits,
1461    ) -> Result<crate::rendering::RenderedText, crate::rendering::RenderError> {
1462        crate::type_rendering::render(self, limits)
1463    }
1464}
1465
1466/// A union's members in the order they are printed. Members are stored in
1467/// canonical order, which puts `null` and `undefined` first; they print last,
1468/// as TypeScript prints them: `string | null | undefined`.
1469pub fn union_display_order(members: &[Type]) -> Vec<&Type> {
1470    let mut ordered: Vec<&Type> = members.iter().collect();
1471    ordered.sort_by_key(|m| match m {
1472        Type::Null => 1,
1473        Type::Undefined => 2,
1474        _ => 0,
1475    });
1476    ordered
1477}
1478
1479impl fmt::Display for Type {
1480    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1481        match self.render_checked(crate::rendering::RenderLimits::default()) {
1482            Ok(rendered) => f.write_str(&rendered.text),
1483            Err(_) => f.write_str("[diagnostic type unavailable]"),
1484        }
1485    }
1486}
1487
1488#[cfg(test)]
1489mod tests {
1490    use super::{Package, Type};
1491
1492    #[test]
1493    fn tuple_mapping_preserves_optional_positions() {
1494        let tuple = super::TupleType {
1495            elements: vec![Type::NumberLiteral(super::LiteralF64(1.0)), Type::Undefined],
1496            optional: 1,
1497        };
1498        let mapped = tuple.map(Type::widen_literal);
1499        assert_eq!(mapped.optional, 1);
1500        assert_eq!(mapped.required_len(), 1);
1501        assert_eq!(mapped.elements, vec![Type::Number, Type::Undefined]);
1502    }
1503
1504    #[test]
1505    fn optional_fields_read_as_undefined() {
1506        let field = super::ObjectField::optional(Type::String);
1507        assert_eq!(
1508            field.read_ty(),
1509            Type::union(vec![Type::String, Type::Undefined])
1510        );
1511    }
1512
1513    #[test]
1514    fn optional_function_and_tuple_display_preserve_omission() {
1515        let ty = Type::Function {
1516            params: vec![Type::union(vec![Type::String, Type::Undefined])],
1517            optional: 1,
1518            ret: Box::new(Type::Void),
1519            predicate: None,
1520            has_rest: false,
1521        };
1522        // An optional parameter admits `undefined` already, as `tsc` prints it.
1523        assert_eq!(ty.to_string(), "(arg0?: string) => void");
1524        let tuple = Type::Tuple(super::TupleType {
1525            elements: vec![
1526                Type::Number,
1527                Type::union(vec![Type::String, Type::Undefined]),
1528            ],
1529            optional: 1,
1530        });
1531        assert_eq!(tuple.to_string(), "[number, (string | undefined)?]");
1532    }
1533
1534    #[test]
1535    fn atomic_display() {
1536        assert_eq!(Type::Number.to_string(), "number");
1537        assert_eq!(Type::String.to_string(), "string");
1538        assert_eq!(Type::Boolean.to_string(), "boolean");
1539        assert_eq!(Type::Null.to_string(), "null");
1540        assert_eq!(Type::Void.to_string(), "void");
1541        assert_eq!(Type::Error.to_string(), "<error>");
1542    }
1543
1544    #[test]
1545    fn readonly_display_matches_typescript() {
1546        let numbers = Type::Array(Box::new(Type::Number));
1547        let readonly = Type::Readonly(Box::new(numbers.clone()));
1548        assert_eq!(readonly.to_string(), "readonly number[]");
1549        assert_eq!(
1550            Type::Array(Box::new(readonly.clone())).to_string(),
1551            "(readonly number[])[]"
1552        );
1553        assert_eq!(
1554            Type::Readonly(Box::new(Type::Array(Box::new(numbers)))).to_string(),
1555            "readonly number[][]"
1556        );
1557        assert_eq!(
1558            Type::Readonly(Box::new(Type::Tuple(
1559                vec![Type::Number, Type::String].into()
1560            )))
1561            .to_string(),
1562            "readonly [number, string]"
1563        );
1564        assert!(readonly.is_readonly_array());
1565        assert_eq!(readonly.peel(), &Type::Array(Box::new(Type::Number)));
1566    }
1567
1568    #[test]
1569    fn string_literal_display_escapes_like_typescript() {
1570        // Each expectation is what `tsc` 5.9 prints for the same literal type.
1571        let cases = [
1572            ("G\"HI\\", r#""G\"HI\\""#),
1573            ("a\nb\r\t", r#""a\nb\r\t""#),
1574            ("\u{8}\u{b}\u{c}", r#""\b\v\f""#),
1575            ("\0x", r#""\0x""#),
1576            ("\u{0}1", r#""\x001""#),
1577            ("\u{7}\u{1b}", r#""\u0007\u001B""#),
1578            ("\u{85}\u{2028}\u{2029}", r#""\u0085\u2028\u2029""#),
1579            ("\u{7f}é", "\"\u{7f}é\""),
1580        ];
1581        for (value, printed) in cases {
1582            assert_eq!(Type::StringLiteral(value.into()).to_string(), printed);
1583        }
1584    }
1585
1586    #[test]
1587    fn structural_equality() {
1588        assert_eq!(Type::Number, Type::Number);
1589        assert_ne!(Type::Number, Type::String);
1590
1591        let a = Type::Function {
1592            params: vec![Type::Number, Type::String],
1593            ret: Box::new(Type::Boolean),
1594            predicate: None,
1595            has_rest: false,
1596            optional: 0,
1597        };
1598        let b = Type::Function {
1599            params: vec![Type::Number, Type::String],
1600            ret: Box::new(Type::Boolean),
1601            predicate: None,
1602            has_rest: false,
1603            optional: 0,
1604        };
1605        let c = Type::Function {
1606            params: vec![Type::Number],
1607            ret: Box::new(Type::Boolean),
1608            predicate: None,
1609            has_rest: false,
1610            optional: 0,
1611        };
1612        assert_eq!(a, b);
1613        assert_ne!(a, c);
1614    }
1615
1616    #[test]
1617    fn function_display_no_params() {
1618        let t = Type::Function {
1619            params: vec![],
1620            ret: Box::new(Type::Number),
1621            predicate: None,
1622            has_rest: false,
1623            optional: 0,
1624        };
1625        assert_eq!(t.to_string(), "() => number");
1626    }
1627
1628    #[test]
1629    fn function_display_one_param() {
1630        let t = Type::Function {
1631            params: vec![Type::Number],
1632            ret: Box::new(Type::Boolean),
1633            predicate: None,
1634            has_rest: false,
1635            optional: 0,
1636        };
1637        assert_eq!(t.to_string(), "(arg0: number) => boolean");
1638    }
1639
1640    #[test]
1641    fn function_display_multi_param() {
1642        let t = Type::Function {
1643            params: vec![Type::Number, Type::String],
1644            ret: Box::new(Type::Void),
1645            predicate: None,
1646            has_rest: false,
1647            optional: 0,
1648        };
1649        assert_eq!(t.to_string(), "(arg0: number, arg1: string) => void");
1650    }
1651
1652    #[test]
1653    fn function_display_nested() {
1654        let inner = Type::Function {
1655            params: vec![],
1656            ret: Box::new(Type::Number),
1657            predicate: None,
1658            has_rest: false,
1659            optional: 0,
1660        };
1661        let outer = Type::Function {
1662            params: vec![],
1663            ret: Box::new(inner),
1664            predicate: None,
1665            has_rest: false,
1666            optional: 0,
1667        };
1668        assert_eq!(outer.to_string(), "() => () => number");
1669    }
1670
1671    #[test]
1672    fn clone_roundtrip() {
1673        let t = Type::Function {
1674            params: vec![Type::Number, Type::String],
1675            ret: Box::new(Type::Boolean),
1676            predicate: None,
1677            has_rest: false,
1678            optional: 0,
1679        };
1680        assert_eq!(t.clone(), t);
1681    }
1682
1683    fn point_type() -> Type {
1684        let mut fields = std::collections::BTreeMap::new();
1685        fields.insert("x".to_string(), crate::ObjectField::required(Type::Number));
1686        fields.insert("y".to_string(), crate::ObjectField::required(Type::Number));
1687        Type::Object {
1688            index: None,
1689            fields,
1690        }
1691    }
1692
1693    #[test]
1694    fn object_structural_equality_ignores_field_insertion_order() {
1695        let mut a = std::collections::BTreeMap::new();
1696        a.insert("y".to_string(), crate::ObjectField::required(Type::Number));
1697        a.insert("x".to_string(), crate::ObjectField::required(Type::Number));
1698
1699        let mut b = std::collections::BTreeMap::new();
1700        b.insert("x".to_string(), crate::ObjectField::required(Type::Number));
1701        b.insert("y".to_string(), crate::ObjectField::required(Type::Number));
1702
1703        assert_eq!(
1704            Type::Object {
1705                index: None,
1706                fields: a
1707            },
1708            Type::Object {
1709                index: None,
1710                fields: b
1711            }
1712        );
1713    }
1714
1715    #[test]
1716    fn object_inequality_on_different_field_set() {
1717        let mut a = std::collections::BTreeMap::new();
1718        a.insert("x".to_string(), crate::ObjectField::required(Type::Number));
1719        let mut b = std::collections::BTreeMap::new();
1720        b.insert("y".to_string(), crate::ObjectField::required(Type::Number));
1721        assert_ne!(
1722            Type::Object {
1723                index: None,
1724                fields: a
1725            },
1726            Type::Object {
1727                index: None,
1728                fields: b
1729            }
1730        );
1731    }
1732
1733    #[test]
1734    fn object_display() {
1735        assert_eq!(point_type().to_string(), "{ x: number; y: number }");
1736        let empty = Type::Object {
1737            index: None,
1738            fields: std::collections::BTreeMap::new(),
1739        };
1740        assert_eq!(empty.to_string(), "{}");
1741    }
1742
1743    #[test]
1744    fn object_display_with_optional_field() {
1745        let mut fields = std::collections::BTreeMap::new();
1746        fields.insert("x".to_string(), crate::ObjectField::required(Type::Number));
1747        fields.insert("y".to_string(), crate::ObjectField::optional(Type::String));
1748        let t = Type::Object {
1749            index: None,
1750            fields,
1751        };
1752        assert_eq!(t.to_string(), "{ x: number; y?: string }");
1753    }
1754
1755    #[test]
1756    fn object_inequality_required_vs_optional() {
1757        let mut a = std::collections::BTreeMap::new();
1758        a.insert("x".to_string(), crate::ObjectField::required(Type::Number));
1759        let mut b = std::collections::BTreeMap::new();
1760        b.insert("x".to_string(), crate::ObjectField::optional(Type::Number));
1761        assert_ne!(
1762            Type::Object {
1763                index: None,
1764                fields: a
1765            },
1766            Type::Object {
1767                index: None,
1768                fields: b
1769            }
1770        );
1771    }
1772
1773    #[test]
1774    fn array_equality() {
1775        assert_eq!(
1776            Type::Array(Box::new(Type::Number)),
1777            Type::Array(Box::new(Type::Number))
1778        );
1779        assert_ne!(
1780            Type::Array(Box::new(Type::Number)),
1781            Type::Array(Box::new(Type::String))
1782        );
1783    }
1784
1785    #[test]
1786    fn array_display() {
1787        assert_eq!(Type::Array(Box::new(Type::Number)).to_string(), "number[]");
1788        assert_eq!(
1789            Type::Array(Box::new(Type::Array(Box::new(Type::Number)))).to_string(),
1790            "number[][]"
1791        );
1792    }
1793
1794    #[test]
1795    fn nested_object_in_array_display() {
1796        let arr = Type::Array(Box::new(point_type()));
1797        assert_eq!(arr.to_string(), "{ x: number; y: number }[]");
1798    }
1799
1800    #[test]
1801    fn type_var_display_uses_source_name() {
1802        assert_eq!(Type::TypeVar("T".to_string()).to_string(), "T");
1803        assert_eq!(Type::TypeVar("Key".to_string()).to_string(), "Key");
1804    }
1805
1806    #[test]
1807    fn type_var_structural_equality_by_name() {
1808        let t = Type::TypeVar("T".to_string());
1809        let t2 = Type::TypeVar("T".to_string());
1810        let u = Type::TypeVar("U".to_string());
1811        assert_eq!(t, t2);
1812        assert_ne!(t, u);
1813        assert_ne!(t, Type::Number);
1814    }
1815
1816    #[test]
1817    fn type_var_inside_array_and_function_displays_through() {
1818        let arr = Type::Array(Box::new(Type::TypeVar("T".to_string())));
1819        assert_eq!(arr.to_string(), "T[]");
1820        let func = Type::Function {
1821            params: vec![Type::TypeVar("T".to_string())],
1822            ret: Box::new(Type::TypeVar("U".to_string())),
1823            predicate: None,
1824            has_rest: false,
1825            optional: 0,
1826        };
1827        assert_eq!(func.to_string(), "(arg0: T) => U");
1828    }
1829
1830    #[test]
1831    fn generic_param_display_uses_name_not_id() {
1832        let gp = Type::GenericParam {
1833            id: 42,
1834            name: "T".to_string(),
1835        };
1836        assert_eq!(gp.to_string(), "T");
1837    }
1838
1839    #[test]
1840    fn generic_param_equality_by_id() {
1841        let a = Type::GenericParam {
1842            id: 5,
1843            name: "T".to_string(),
1844        };
1845        let b = Type::GenericParam {
1846            id: 5,
1847            name: "T".to_string(),
1848        };
1849        assert_eq!(a, b);
1850
1851        let c = Type::GenericParam {
1852            id: 6,
1853            name: "T".to_string(),
1854        };
1855        assert_ne!(a, c);
1856    }
1857
1858    #[test]
1859    fn union_canonical_dedup() {
1860        let t = Type::union(vec![Type::Number, Type::Null, Type::Number]);
1861        let Type::Union(members) = t else {
1862            panic!("expected Union, got {t:?}");
1863        };
1864        assert_eq!(members, vec![Type::Number, Type::Null]);
1865    }
1866
1867    #[test]
1868    fn union_canonical_order_independent() {
1869        let a = Type::union(vec![Type::Number, Type::Null]);
1870        let b = Type::union(vec![Type::Null, Type::Number]);
1871        assert_eq!(a, b);
1872    }
1873
1874    #[test]
1875    fn union_collapses_to_single_member() {
1876        let t = Type::union(vec![Type::Number, Type::Number]);
1877        assert_eq!(t, Type::Number);
1878    }
1879
1880    #[test]
1881    fn union_flattens_nested() {
1882        let inner = Type::union(vec![Type::String, Type::Null]);
1883        let outer = Type::union(vec![Type::Number, inner]);
1884        let expected = Type::union(vec![Type::Number, Type::String, Type::Null]);
1885        assert_eq!(outer, expected);
1886    }
1887
1888    #[test]
1889    fn union_error_member_collapses_to_error() {
1890        let t = Type::union(vec![Type::Number, Type::Error]);
1891        assert_eq!(t, Type::Error);
1892    }
1893
1894    #[test]
1895    fn union_display_basic() {
1896        let t = Type::union(vec![Type::Number, Type::String]);
1897        assert_eq!(t.to_string(), "number | string");
1898    }
1899
1900    #[test]
1901    fn union_display_with_null() {
1902        let t = Type::union(vec![Type::Number, Type::Null]);
1903        assert_eq!(t.to_string(), "number | null");
1904        let t2 = Type::union(vec![Type::Null, Type::Number]);
1905        assert_eq!(t2.to_string(), "number | null");
1906    }
1907
1908    #[test]
1909    fn union_display_wraps_function_member() {
1910        let fn_ty = Type::Function {
1911            params: vec![],
1912            ret: Box::new(Type::String),
1913            predicate: None,
1914            has_rest: false,
1915            optional: 0,
1916        };
1917        let t = Type::union(vec![fn_ty, Type::Boolean]);
1918        assert_eq!(t.to_string(), "boolean | (() => string)");
1919    }
1920
1921    use super::LiteralF64;
1922
1923    #[test]
1924    fn string_literal_display_is_quoted() {
1925        let t = Type::StringLiteral("north".to_string());
1926        assert_eq!(t.to_string(), "\"north\"");
1927    }
1928
1929    #[test]
1930    fn number_literal_display_is_unquoted_with_whole_number_shape() {
1931        let whole = Type::NumberLiteral(LiteralF64(42.0));
1932        assert_eq!(whole.to_string(), "42");
1933        let frac = Type::NumberLiteral(LiteralF64(4.5));
1934        assert_eq!(frac.to_string(), "4.5");
1935    }
1936
1937    #[test]
1938    fn string_literal_equality_by_value() {
1939        let a = Type::StringLiteral("hi".to_string());
1940        let b = Type::StringLiteral("hi".to_string());
1941        let c = Type::StringLiteral("bye".to_string());
1942        assert_eq!(a, b);
1943        assert_ne!(a, c);
1944    }
1945
1946    #[test]
1947    fn number_literal_equality_by_value() {
1948        let a = Type::NumberLiteral(LiteralF64(42.0));
1949        let b = Type::NumberLiteral(LiteralF64(42.0));
1950        let c = Type::NumberLiteral(LiteralF64(43.0));
1951        assert_eq!(a, b);
1952        assert_ne!(a, c);
1953    }
1954
1955    #[test]
1956    fn union_sorts_literals_next_to_base_types() {
1957        // NumberLiteral sorts after Number, StringLiteral after String per variant order.
1958        let t = Type::union(vec![
1959            Type::String,
1960            Type::StringLiteral("hi".to_string()),
1961            Type::Number,
1962            Type::NumberLiteral(LiteralF64(42.0)),
1963        ]);
1964        assert_eq!(t.to_string(), "number | 42 | string | \"hi\"");
1965    }
1966
1967    #[test]
1968    fn widen_literal_replaces_a_literal_with_its_base() {
1969        assert_eq!(
1970            Type::NumberLiteral(LiteralF64(1.0)).widen_literal(),
1971            Type::Number
1972        );
1973        assert_eq!(
1974            Type::StringLiteral("hi".to_string()).widen_literal(),
1975            Type::String
1976        );
1977    }
1978
1979    /// A union widens memberwise, and `Type::union` then deduplicates — so a union of
1980    /// literals over one base collapses to that base rather than repeating it.
1981    #[test]
1982    fn widen_literal_collapses_a_union_of_literals() {
1983        let t = Type::union(vec![
1984            Type::NumberLiteral(LiteralF64(1.0)),
1985            Type::NumberLiteral(LiteralF64(2.0)),
1986        ]);
1987        assert_eq!(t.widen_literal(), Type::Number);
1988    }
1989
1990    /// Enums are nominal, not literal: widening one would discard the identity its
1991    /// members are checked against. `primitive_behavior` is the enum-aware accessor.
1992    #[test]
1993    fn widen_literal_leaves_everything_else_alone() {
1994        assert_eq!(Type::Number.widen_literal(), Type::Number);
1995        assert_eq!(Type::Boolean.widen_literal(), Type::Boolean);
1996        let arr = Type::Array(Box::new(Type::NumberLiteral(LiteralF64(1.0))));
1997        assert_eq!(arr.widen_literal(), arr);
1998    }
1999
2000    #[test]
2001    fn union_of_two_string_literals_sorts_by_value() {
2002        let t = Type::union(vec![
2003            Type::StringLiteral("south".to_string()),
2004            Type::StringLiteral("north".to_string()),
2005        ]);
2006        assert_eq!(t.to_string(), "\"north\" | \"south\"");
2007    }
2008
2009    #[test]
2010    fn literal_f64_equality_distinguishes_negative_zero_at_bit_level() {
2011        // Bit-pattern equality distinguishes -0.0 and +0.0; parser canonicalizes -0.0→0.0 so
2012        // this case won't appear in practice.
2013        let pos = LiteralF64(0.0);
2014        let neg = LiteralF64(-0.0);
2015        assert_ne!(pos, neg);
2016    }
2017
2018    #[test]
2019    fn type_var_and_generic_param_are_distinct_types() {
2020        // TypeVar is signature form; GenericParam is body form.
2021        // Even with the same name they're different variants.
2022        let tv = Type::TypeVar("T".to_string());
2023        let gp = Type::GenericParam {
2024            id: 0,
2025            name: "T".to_string(),
2026        };
2027        assert_ne!(tv, gp);
2028    }
2029
2030    #[test]
2031    fn alias_display_renders_name_not_body() {
2032        let ty = user_alias("Circle", point_type());
2033        assert_eq!(ty.to_string(), "Circle");
2034    }
2035
2036    #[test]
2037    fn peel_walks_through_alias_to_underlying() {
2038        let inner = Type::Number;
2039        let aliased = user_alias("ID", inner.clone());
2040        assert_eq!(aliased.peel(), &inner);
2041    }
2042
2043    #[test]
2044    fn peel_is_idempotent_through_nested_aliases() {
2045        let nested = user_alias("A", user_alias("B", Type::Number));
2046        assert_eq!(nested.peel(), &Type::Number);
2047        assert_eq!(nested.peel().peel(), &Type::Number);
2048    }
2049
2050    #[test]
2051    fn alias_is_not_structurally_equal_to_underlying() {
2052        let aliased = user_alias("ID", Type::Number);
2053        assert_ne!(aliased, Type::Number);
2054        assert_eq!(aliased.peel(), &Type::Number);
2055    }
2056
2057    #[test]
2058    fn alias_routes_to_underlying_interface() {
2059        let aliased = user_alias("ID", Type::Number);
2060        let (_mangled, _pkg, iface, _args) = aliased.interface_routing().expect("number routes");
2061        assert_eq!(iface, "Number");
2062    }
2063
2064    #[test]
2065    fn union_of_error_alias_collapses_to_error() {
2066        let alias_err = user_alias("Bad", Type::Error);
2067        let t = Type::union(vec![Type::Number, alias_err]);
2068        assert_eq!(t, Type::Error);
2069    }
2070
2071    #[test]
2072    fn union_preserves_distinct_alias_members_for_display() {
2073        let circle = user_alias("Circle", point_type());
2074        let rect = user_alias("Rectangle", Type::Number);
2075        let t = Type::union(vec![circle, rect]);
2076        // Ordered by the peeled bodies, which puts `Number` before the object
2077        // shape — not by the order the members were written.
2078        assert_eq!(t.to_string(), "Rectangle | Circle");
2079    }
2080
2081    #[test]
2082    fn union_dedups_an_alias_against_its_own_body_keeping_the_label() {
2083        let n = user_alias("N", Type::Number);
2084        assert_eq!(Type::union(vec![n.clone(), Type::Number]), n);
2085        assert_eq!(Type::union(vec![Type::Number, n.clone()]), n);
2086    }
2087
2088    #[test]
2089    fn union_dedups_aliases_that_share_one_body() {
2090        let circle = user_alias("Circle", point_type());
2091        let rect = user_alias("Rectangle", point_type());
2092        assert_eq!(Type::union(vec![circle.clone(), rect]), circle);
2093    }
2094
2095    #[test]
2096    fn union_flattens_an_alias_whose_body_is_a_union() {
2097        let nullable = user_alias("MN", Type::union(vec![Type::Number, Type::Null]));
2098        assert_eq!(
2099            Type::union(vec![nullable, Type::Number]),
2100            Type::union(vec![Type::Number, Type::Null])
2101        );
2102    }
2103
2104    #[test]
2105    fn generic_alias_display_includes_args() {
2106        let ty = user_alias_args("Box", vec![Type::Number], Type::Number);
2107        assert_eq!(ty.to_string(), "Box<number>");
2108    }
2109
2110    fn iface(package: &str, name: &str) -> Type {
2111        Type::interface_ref(
2112            Package(package.to_string()),
2113            name,
2114            crate::mangle::package_symbol(package, name),
2115            Vec::new(),
2116        )
2117    }
2118
2119    fn user_alias(name: &str, ty: Type) -> Type {
2120        user_alias_args(name, Vec::new(), ty)
2121    }
2122
2123    fn user_alias_args(name: &str, args: Vec<Type>, ty: Type) -> Type {
2124        Type::alias_ty(
2125            Package::user(),
2126            name,
2127            crate::mangle::package_symbol(crate::mangle::USER_PACKAGE, name),
2128            args,
2129            Box::new(ty),
2130        )
2131    }
2132
2133    #[test]
2134    fn mangled_name_keys_type_identity() {
2135        // Identity is the mangled name. Same `(package, name)` ⇒ same mangled ⇒ equal;
2136        // a different owning package ⇒ different mangled ⇒ distinct (the multi-file
2137        // property: two same-named interfaces in different packages/modules are not
2138        // conflated).
2139        assert_eq!(iface("main", "Response"), iface("main", "Response"));
2140        let http = iface("submilli:http", "Response");
2141        let main = iface("main", "Response");
2142        assert_ne!(
2143            http, main,
2144            "different package ⇒ different mangled ⇒ distinct"
2145        );
2146        assert_ne!(
2147            http.cmp(&main),
2148            std::cmp::Ordering::Equal,
2149            "Ord must distinguish distinct mangled names"
2150        );
2151        assert_ne!(
2152            iface("main", "Response"),
2153            iface("main", "Other"),
2154            "different names stay distinct"
2155        );
2156    }
2157
2158    #[test]
2159    fn same_name_different_module_not_equal_nor_assignable() {
2160        // The core multi-file guarantee: a sibling module's `Logger`
2161        // (`mod:main#util#Logger`) is a distinct type from the root's `Logger`
2162        // (`main#Logger`), even though the bare name matches.
2163        let root = iface("main", "Logger");
2164        let sibling = Type::InterfaceRef {
2165            mangled: crate::mangle::package_module_symbol("main", "util", "Logger"),
2166            package: Package::user(),
2167            name: "Logger".to_string(),
2168            args: Vec::new(),
2169        };
2170        assert_ne!(root, sibling);
2171        // Same mangled ⇒ equal.
2172        let root2 = iface("main", "Logger");
2173        assert_eq!(root, root2);
2174    }
2175
2176    #[test]
2177    fn union_dedups_interface_refs_by_mangled() {
2178        // Same mangled collapses to one member; different mangled stays a 2-member union.
2179        let same = Type::union(vec![iface("main", "Response"), iface("main", "Response")]);
2180        assert!(
2181            !matches!(same, Type::Union(_)),
2182            "identical refs collapse to one member, got {same:?}"
2183        );
2184        let distinct = Type::union(vec![
2185            iface("submilli:http", "Response"),
2186            iface("main", "Response"),
2187        ]);
2188        assert!(
2189            matches!(distinct, Type::Union(ref ms) if ms.len() == 2),
2190            "distinct-package refs stay a 2-member union, got {distinct:?}"
2191        );
2192    }
2193
2194    #[test]
2195    fn generic_alias_display_multiple_args() {
2196        let ty = user_alias_args("Pair", vec![Type::String, Type::Number], Type::Number);
2197        assert_eq!(ty.to_string(), "Pair<string, number>");
2198    }
2199}