Question
How to make type inference work on a piece of code?
I have this (simplified) piece of code:
export abstract class Scheduler<TPayload> {}
export interface EventKey<T> extends Symbol {}
export type SystemSpecification<TPayload> = (
args: {
/** The value provided by current scheduler tick */
payload: TPayload;
},
) => void;
export function defineSystem<TPayload>(
system: SystemSpecification<TPayload>,
scheduler: new () => Scheduler<TPayload>
) {
// ...
}
const on = <TPayload>(eventKey: EventKey<TPayload>) =>
class extends Scheduler<TPayload> {
// ...
};
const clickEvent = Symbol('clickEvent') as EventKey<{ foo: 20 }>;
defineSystem(
({ payload }) => console.log(payload.foo),
on(clickEvent)
);
I am expecting payload
value to be infered from clickEvent
type : {foo: number}
, but instead, I have an error: 'payload' is of type unknown
Am I doing something wrong? Is it actually possible to infer payload from clickEvent type?
2 49
2