#Set type based on super call

6 messages · Page 1 of 1 (latest)

leaden marsh
#
interface Params {
  // This is what I have right now
  model: MsgModel | CmdModel | VcModel
}
class Example extends SomeClass {
  constructor() {
    super({
      name: 'example',
      type: 'MSG' // 'MSG' | 'CMD' | 'VC'
    })
    
  }
  async run({ model }: Params) {
    ...
  }
}

In the above class, how do I set the type of model param based on the value of type in the super call? I want the model to be MsgModel if type is MSG, CmdModel is the type is VC, etc. Here's an example of what SomeClass looks like:

class SomeClass {
  constructor({ name, type }: { name: string, type: 'MSG' | 'CMD' | 'VC' }) {
    this.name = name;
    this.type = type;
  }
  async run(_params: Params) {
     ...
   }
}
wraith plinth
#

you could make the class and type generic have a mapping between the type values and the model property

wraith plinth
#

something like this for example

dark scarabBOT
leaden marsh
# wraith plinth something like this for example

Hey, so can I make the generic type optional here?

class SomeClass<T extends keyof ModelMap> {
    name: string;
    type: T;
    constructor({ name, type }: { name: string, type: T }) {
        this.name = name;
        this.type = type;
    }
    async run(_params: Params<T>) {
        // ...
    }
}

In some case, a boolean (let's call it application) can be false, where the type is not required.