Signal passed as component prop gives error 'Type 'ReadonlySignal<number>' is not assignable to type 'number'.ts(2322)' #653
-
Hello everyone,
This is happening because I am now passing a signal as a prop to something that used to be a simple number. Example: import { computed } from '@preact/signals';
...
const progressValue = computed(() => getProgressTimeInteger(playbackTimeline.value, playerData.value) )
...
<ProgressBar progress={progressValue as unknown as number} /> How should I properly handle this kind of scenario? ps: I found this other discussion, but I am not sure that helps me because I am NOT using React, but just Preact, so I don't think I need the mentioned babel plugin. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 13 replies
-
You must unwrap the signal then. The optimization can only work if the consuming component uses signals, if it doesn't, there's no way around unwrapping.
This will break the second the component tries to use that prop in an expression, definitely don't do that. Silly example: function MyComponent({ foo }: { foo: number }) {
if (foo == 3) return <p>Is 3</p>;
return <p>{foo}</p>;
} |
Beta Was this translation helpful? Give feedback.
-
you can use this functions to unwrap the signals for consumption? import { Signal } from '@preact/signals';
import type { JSX } from 'preact';
export function access<T extends unknown>(value: JSX.Signalish<T>): T {
return value instanceof Signal ? value.value : value;
}
export function accessArray<T extends unknown>(list: ReadonlyArray<JSX.Signalish<T>>): Array<T> {
return list.map((value) => access(value));
} |
Beta Was this translation helpful? Give feedback.
you can use this functions to unwrap the signals for consumption?