Skip to content

Commit

Permalink
feat(core): expose UnknownActorRef
Browse files Browse the repository at this point in the history
`AnyActorRef` is problematic for some use cases, because the result of the `AnyActorRef['getSnapshot']` function is of type `any`.

However, changing `AnyActorRef` to use a narrow type in `ActorRef`'s first type argument, `TSnapshot`, breaks conditional types which perform inference based on `TSnapshot`.

This change introduces `UnknownActorRef`, which is like `AnyActorRef`, but is not intended to be inferred from in future conditional types. A consumer can use `UnknownActorRef` like so:

```ts
const actor: UnknownActorRef = getSomeActor();

// inferred as `Snapshot<unknown>`
const snapshot = actor.getSnapshot();

// do things w/ snapshot
```

`AnyActorRef` would return `any` from `actor.getSnapshot()`, which make it unsuitable for this use-case in a strictly-typed environment.
  • Loading branch information
boneskull committed Jun 15, 2024
1 parent 28d437c commit a1ca784
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,8 @@ export interface ActorRef<

export type AnyActorRef = ActorRef<any, any, any>;

export type UnknownActorRef = ActorRef<Snapshot<unknown>, EventObject>;

export type ActorLogicFrom<T> = ReturnTypeOrValue<T> extends infer R
? R extends StateMachine<
any,
Expand Down
17 changes: 15 additions & 2 deletions packages/core/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { from } from 'rxjs';
import { log } from '../src/actions/log';
import { raise } from '../src/actions/raise';
import { stopChild } from '../src/actions/stopChild';
import { PromiseActorLogic, fromCallback, fromPromise } from '../src/actors';
import {
PromiseActorLogic,
createEmptyActor,
fromCallback,
fromPromise
} from '../src/actors';
import {
ActorRefFrom,
InputFrom,
Expand All @@ -19,7 +24,8 @@ import {
spawnChild,
stateIn,
setup,
toPromise
toPromise,
UnknownActorRef
} from '../src/index';

function noop(_x: unknown) {
Expand Down Expand Up @@ -4567,3 +4573,10 @@ it('fromPromise should not have issues with actors with emitted types', () => {

toPromise(actor);
});

it('UnknownActorRef should return a Snapshot-typed value from getSnapshot()', () => {
const actor: UnknownActorRef = createEmptyActor();

// @ts-expect-error
actor.getSnapshot().status === 'FOO';
});

0 comments on commit a1ca784

Please sign in to comment.