const BaseEvent = type({
type: "string",
timestamp: "number",
});
const SomeEvent = BaseEvent.and({
type: "'some_event'",
data: {
foo: "string",
},
});
const SomePipedEvent = BaseEvent.and({
type: "'some_piped_event'",
data: {
bar: "string",
},
}).pipe((o) => {
return {
...o,
data: {
...o.data,
someExtraField: 5,
},
};
});
class EventListener<T extends typeof BaseEvent> {
constructor(events: T[]) {
}
public on(event: T['infer']['type'], callback: (event: T['infer']) => void) {
}
}
/* All the code below has type errors */
const listener = new EventListener([SomeEvent, SomePipedEvent]);
listener.on("some_event", (event) => {
event.data.foo
});
listener.on("some_piped_event", (event) => {
event.data.someExtraField
});
const BaseEvent = type({
type: "string",
timestamp: "number",
});
const SomeEvent = BaseEvent.and({
type: "'some_event'",
data: {
foo: "string",
},
});
const SomePipedEvent = BaseEvent.and({
type: "'some_piped_event'",
data: {
bar: "string",
},
}).pipe((o) => {
return {
...o,
data: {
...o.data,
someExtraField: 5,
},
};
});
class EventListener<T extends typeof BaseEvent> {
constructor(events: T[]) {
}
public on(event: T['infer']['type'], callback: (event: T['infer']) => void) {
}
}
/* All the code below has type errors */
const listener = new EventListener([SomeEvent, SomePipedEvent]);
listener.on("some_event", (event) => {
event.data.foo
});
listener.on("some_piped_event", (event) => {
event.data.someExtraField
});