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

Computed is not assignable to computed with broader type #7

Open
temoncher opened this issue May 15, 2024 · 0 comments
Open

Computed is not assignable to computed with broader type #7

temoncher opened this issue May 15, 2024 · 0 comments

Comments

@temoncher
Copy link

temoncher commented May 15, 2024

I work on a small form library based on signals and discovered an annoying issue with computeds

import { Signal } from 'signal-polyfill';

interface Broader {
  strProp: string;
}

interface Narrower extends Broader {
  numProp: number;
}

const state = new Signal.State<Narrower>({
  strProp: '',
  numProp: 33,
});

// not assignable, but should be, right?
const derived: Signal.Computed<Broader> = new Signal.Computed(() => state.get());

It impairs ability to consume computed signals, for example:

interface FormState<T> {
  value: T;
  errors: unknown;
}

class FormControl<T> {
  #state: Signal.State<FormState<T>>;
  readonly $state = new Signal.Computed(() => this.#state.get());
  constructor(intiValue: T) {
    this.#state = new Signal.State<FormState<T>>({
      value: intiValue,
      errors: null,
    });
  }
}

const numControl = new FormControl(0);

function valueConsumer(onlyValueState: Signal.Computed<{ value: number }>) {
  console.log(onlyValueState.get().value);
}

// errors, Argument of type 'Computed<FormState<number>>' is not assignable to parameter of type 'Computed<{ value: number; }>'
valueConsumer(numControl.$state);

repro: https://stackblitz.com/edit/vitejs-vite-jueqfh?file=src%2Fmain.ts

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

No branches or pull requests

1 participant