[Solved] how can I implement types on classes?

Meno-101 Asks: how can I implement types on classes?
I have this basic class:

type Action = { type: string; payload?: {} }
type Dispatch<ActionType extends Action> = (action: ActionType) => void

type Actions = 
  |  { type: '@ADD', payload: { item: string, id: string } }
  |  { type: '@REM', payload: { id: string } }

class CardActions implements ActionClass<Actions> { // this isn't working as I expect, see below
  constructor(private dispatch: Dispatch<Actions>) {}

  disp(action) {} // this isn't working

trying to implement an interface didn’t work neither for the constructor nor the disp function, it always has the error:

Index signature is missing in type ‘CardsActions’


Class ‘CardActions’ incorrectly implements interface ‘ActionClass’.

export interface ActionClass<ActionType extends Action> {
  // new(dispatch: string): any;
  [actionKey: string]: (action: ActionType['type']) => void

I’m expecting it to work like any function, did I misunderstand it?


interface Func {
  (action: Actions['type']): void

const func: Func = (action) => {} // this will work


why they don’t work like eachother? and how can I make it work?

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.