Skip to content

fix: rework generated types to minimise variation in type union#430

Merged
Julusian merged 1 commit into
mainfrom
fix/new-device-type-issue
Feb 12, 2026
Merged

fix: rework generated types to minimise variation in type union#430
Julusian merged 1 commit into
mainfrom
fix/new-device-type-issue

Conversation

@Julusian

@Julusian Julusian commented Feb 11, 2026

Copy link
Copy Markdown
Member

About the Contributor

This pull request is posted on behalf of the BBC

Type of Contribution

This is a: Bug fix

Current Behavior

This was a weird one, we appear to be hitting a limit on the size of a type union that appears as soon as one more device type is added:

image

Deleting any of the existing integration types makes the error go away.
Updating typescript to 5.7 did not resolve it.

Seems to be a designed limitation in typescript: microsoft/TypeScript#40803
Not 100% sure what this change does that avoids that, but it avoids the issue

New Behavior

Instead this reworks the types a little bit, which makes the error go away. Other than a few changes in generics, functionally nothing is really different.

We are not using this Base type in sofie-core, so this should have minimal impact outside of TSR.

Testing Instructions

Other Information

Status

  • PR is ready to be reviewed.
  • The functionality has been tested by the author.
  • Relevant unit tests has been added / updated.
  • Relevant documentation (code comments, system documentation) has been added / updated.

Problem

TypeScript compilation was hitting a type union complexity limit where adding one more device type triggered a cascade of compilation errors. These errors surfaced in ConnectionManager.ts with multiple TS2345 type assignment incompatibilities. Deleting existing device types would eliminate the errors, and upgrading to TypeScript 5.7 did not resolve the issue. The root cause was excessive variation in how DeviceOptionsBase type unions were being formed across the codebase.

Solution

Reworked generated type definitions to minimize variation in type unions by making the generic type parameterization more explicit and consistent throughout the codebase.

Key Changes

Type Definition Changes (packages/timeline-state-resolver-types/src/device.ts)

  • Refactored DeviceOptionsBase from a single generic parameter (T) to two explicit parameters: TType extends DeviceType and TOptions
  • Updated property signatures:
    • type: DeviceTypetype: TType
    • options?: Toptions?: TOptions

Generated Type Schema (packages/timeline-state-resolver-tools/bin/schema-types.mjs)

  • Added Type field to generated per-directory DeviceTypes interfaces
  • Converted per-directory DeviceOptions declarations from interfaces to type aliases using the new DeviceOptionsBase<DeviceType.{DeviceTypeId}, {Dir}Options> form
  • Added import of DeviceType to generated output files

Type Propagation Updates

  • device.ts: Updated Device and DeviceWithState generic constraints to include Type: DeviceType in the DeviceTypes constraint and pass both DeviceTypes['Type'] and DeviceTypes['Options'] to DeviceOptionsBase
  • conductor.ts: Updated internal function signatures to use DeviceOptionsBase<any, any> consistently across _mapAllConnections, device mapping, and filtering operations
  • deviceContainer.ts: Updated DeviceContainer and its static create method to use the two-parameter DeviceOptionsBase<DeviceType, unknown>
  • ConnectionManager.ts: Standardized all public event signatures and method return types to use DeviceOptionsBase<any, any>
  • remoteDeviceInstance.ts: Updated BaseRemoteDeviceIntegration and RemoteDeviceInstance generic constraints to use DeviceOptionsBase<any, any>

Impact

  • Functional behavior is unchanged; this is purely a type system restructuring
  • The DeviceOptionsBase base type changes are isolated within TSR and should have minimal impact on sofie-core, which does not directly depend on this base type
  • Resolves the TypeScript compilation errors in ConnectionManager.ts by reducing union type complexity

@Julusian Julusian requested a review from a team as a code owner February 11, 2026 17:53
@Julusian Julusian added the contribution from BBC Contributions sponsored by BBC (bbc.co.uk) label Feb 11, 2026
@coderabbitai

coderabbitai Bot commented Feb 11, 2026

Copy link
Copy Markdown

Walkthrough

This change refactors the DeviceOptionsBase generic type system from a single type parameter to two type parameters (TType extending DeviceType and TOptions), and updates code generation to expose a discriminated Type property in generated device type interfaces. The changes propagate this generic signature update across conductor, device, container, and connection management classes.

Changes

Cohort / File(s) Summary
Code Generation
packages/timeline-state-resolver-tools/bin/schema-types.mjs
Adds DeviceType import to generated output and introduces Type field to {dirId}DeviceTypes interface. Converts DeviceOptions{Dir} from interface extending DeviceOptionsBase to type alias using DeviceOptionsBase with two type parameters.
Base Type Definition
packages/timeline-state-resolver-types/src/device.ts
Updates DeviceOptionsBase interface signature from single generic T to dual generics TType extends DeviceType and TOptions, adjusting type and options properties accordingly.
Conductor & Runtime
packages/timeline-state-resolver/src/conductor.ts
Propagates DeviceOptionsBase generic signature expansion across _mapAllConnections, prepareForHandleState, push-state, clearFuture, and filterLayersPerDevice call sites.
Device Abstractions
packages/timeline-state-resolver/src/devices/device.ts, packages/timeline-state-resolver/src/devices/deviceContainer.ts
Updates IDevice, Device, and DeviceWithState interfaces to require DeviceTypes with Type field; expands DeviceOptionsBase constraints to two type parameters. Adds DeviceType import to DeviceContainer.
Connection Management
packages/timeline-state-resolver/src/service/ConnectionManager.ts, packages/timeline-state-resolver/src/service/remoteDeviceInstance.ts
Updates public event signatures (connectionAdded), method return types (getConnections, getConnection), and factory signatures (createContainer, RemoteDeviceInstance.create) to use expanded DeviceOptionsBase generics.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

bug, Contribution from BBC

Poem

🐰 With whiskers twitching, types align,
Two parameters in perfect line,
DeviceOptionsBase now more refined,
Discriminated types, strongly signed!
The timeline hops with better type,
Generic shapes now truly ripe! 🎯

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: reworking generated types to minimize type union variation, which directly addresses the TypeScript compilation issue mentioned in the PR objectives.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/new-device-type-issue

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud

Copy link
Copy Markdown

@nytamin nytamin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm this solves an issue for me as well!

@Julusian Julusian merged commit 0a476f2 into main Feb 12, 2026
41 checks passed
This was referenced Feb 25, 2026
@coderabbitai coderabbitai Bot mentioned this pull request Apr 27, 2026
4 tasks
@nytamin nytamin deleted the fix/new-device-type-issue branch June 16, 2026 05:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contribution from BBC Contributions sponsored by BBC (bbc.co.uk)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants