Skip to content

TypeSchema: Refactor type system for discriminated unions and type-safe resolution#113

Merged
ryukzak merged 7 commits intomainfrom
refactoring-typeschema-details
Mar 24, 2026
Merged

TypeSchema: Refactor type system for discriminated unions and type-safe resolution#113
ryukzak merged 7 commits intomainfrom
refactoring-typeschema-details

Conversation

@ryukzak
Copy link
Copy Markdown
Collaborator

@ryukzak ryukzak commented Mar 23, 2026

Type system improvements

  • Make SpecializationTypeSchema a proper discriminated union (ResourceTypeSchema | ComplexTypeTypeSchema | LogicalTypeSchema)
  • Make RichFHIRSchema a discriminated union on both derivation and kind
    • Split RichSpecializationFHIRSchema into RichPrimitiveFHIRSchema, RichComplexTypeFHIRSchema, RichResourceFHIRSchema, RichLogicalFHIRSchema
    • Add mkIdentifier overloads for each named type
  • Extract TypeFamily type, FHIRSchemaKind type, TypeSchemaGuardInput alias

Code simplification

  • Simplify transformFhirSchema with early returns instead of mutable let typeSchema
  • Replace identifier kind checks with type schema guards in ViewModelFactory
  • Simplify findLastSpecializationByIdentifier with isNestedTypeSchema guard

Type-safe resolution

  • Split resolve into resolve(Identifier) → TypeSchema and resolveType(TypeIdentifier) → TypeSchema | NestedTypeSchema
  • Remove type casts from resolveByUrl — returns TypeSchema | NestedTypeSchema | undefined
  • Add isNestedTypeSchema guard for narrowing resolution results
  • Migrate callers from resolve to resolveType where TypeIdentifier is used

@ryukzak ryukzak force-pushed the refactoring-typeschema-details branch from fb3b588 to 14ba943 Compare March 23, 2026 12:41
@ryukzak ryukzak changed the title TypeSchema: Split RegularTypeSchema into typed variants and rename to SpecializationTypeSchema TypeSchema: Split SpecializationTypeSchema into typed variants Mar 23, 2026
@ryukzak ryukzak force-pushed the refactoring-typeschema-details branch from d259163 to f297d9d Compare March 24, 2026 12:25
ryukzak added 6 commits March 24, 2026 16:35
…a, LogicalTypeSchema

- Export LogicalIdentifier, define three typed variants sharing RegularTypeSchemaBody
- Redefine RegularTypeSchema as union alias of the three variants
- Narrow type guard return types (isResourceTypeSchema, isComplexTypeTypeSchema, isLogicalTypeSchema)
- Narrow collect functions in TypeSchemaIndex (collectResources, collectComplexTypes, collectLogicalModels)
- Make groupByPackages generic to preserve narrowed types
- Type nestedIndex as NestedType instead of TypeSchema
- Use isSpecializationTypeSchema guard in TS writer instead of string array check
- Split RichFHIRSchema into RichProfileFHIRSchema (derivation: "constraint")
  and RichSpecializationFHIRSchema (derivation: "specialization")
- Split RichSpecializationFHIRSchema into per-kind members:
  RichPrimitiveFHIRSchema, RichComplexTypeFHIRSchema,
  RichResourceFHIRSchema, RichLogicalFHIRSchema
- enrichFHIRSchema normalizes derivation and kind at the boundary
- mkIdentifier overloads now use named union members, enabling
  control-flow narrowing to resolve the correct return type
- transformer branches on derivation/kind directly, removing
  runtime asserts that the type system now enforces
…ifier)

- resolve(id: Identifier) returns TypeSchema only, no nested
- resolveType(id: TypeIdentifier) returns TypeSchema | NestedTypeSchema
- nestedIndex properly typed as Record<..., NestedTypeSchema>
- Callers passing TypeIdentifier migrated to resolveType
@ryukzak ryukzak force-pushed the refactoring-typeschema-details branch from 098c0f3 to ddb1e6f Compare March 24, 2026 15:36
@ryukzak ryukzak changed the title TypeSchema: Split SpecializationTypeSchema into typed variants TypeSchema: Refactor type system for discriminated unions and type-safe resolution Mar 24, 2026
@ryukzak ryukzak merged commit 183067b into main Mar 24, 2026
31 checks passed
@ryukzak ryukzak deleted the refactoring-typeschema-details branch March 24, 2026 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant