Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contravariant Msg type in Dispatchable and LocalActorRef #148

Open
wants to merge 3 commits into
base: next
Choose a base branch
from

Conversation

le0-0
Copy link

@le0-0 le0-0 commented Jan 22, 2025

Makes LocalActorRef and Dispatchable contravariant.

Description

The type of the protocol field of Dispatchable was changed to a type that TypeScript recognizes as contravariant. This field doesn't seem to be used for anything else beside coaxing the type system. Please deny the PR if this assumption is wrong.

Fixing this surfaced an error related to the fact that StatelessActorProps was not exactly assignable to ActorProps, even though it probably should be. I fixed this as well.

Related Issue

Fixes #147

Motivation and Context

The error, as outlined in the #147, causes the type system to disallow certain assigning statements that should be allowed and vice versa.

How Has This Been Tested?

  • Wrote a new unit test in actor.test.ts that creates a scenario where the contravariance of LocalActorRef is put to the test.
    • It makes use of // ts-expect-error
  • Ran npm run test in @nact/core
  • Ran npm run typecheck in @nact/core

Screenshots (if appropriate):

The new test case I wrote went from this:
image

to this:
image

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

I guess the interface for StatelessActorProps was changed slightly, so if this is actually used in explicit form anywhere. Like a field like this, let actorProps: StatelessActorProps<LocalActorSystemRef> = ..., that line will fail. I find that it was basically a bug that the line would be accepted anyway because there is no such signature for ActorProps (one without State and Msg). However, with the logic that existing code that would work now would end up not compiling, it could be classified as a breaking change.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document. (I assume this has been renamed to CODE_OF_CONDUCT).
  • I have added tests to cover my changes.
  • All new and existing tests passed.

le0-0 added 3 commits January 22, 2025 16:05
The tests are written like other jest tests but will fail on 'npm run typecheck' instead of 'npm run test'.
This caused StatelessActorProps to be considered <Unknown, ParentRef> instead of <Msg, ParentRef> in some cases.
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.

TypeScript thinks Msg type parameter of Dispatchable is covariant, but it should be contravariant
1 participant