There are like these types
interface A {
id: number
name: string
patternA: patternA[]
}
interface B {
id: number
name: string
patternB: patternB[]
}
interface C {
id: number
name: string
patternC: patternC[]
city: string
}
and there is factory method
const factory = (type, data: /** 🤔 what should I write?? **/) => {
switch (type) {
case 'first':
return createHogeA(data)
case 'second':
return createHogeB(data)
case 'third':
return createHogeC(data)
default:
return
}
}
const createHogeA = (data: A) => somefunctionA(data)
const createHogeB = (data: B) => somefunctionB(data)
const createHogeC = (data: C) => somefunctionC(data)
What I've tried so far
- Set type like this, but then appears error because it's possible that each datas type could pass to each functions
const factory = (type, data: A | B | C) => {
switch (type) {
case 'first':
return createHogeA(data)
case 'second':
return createHogeB(data)
case 'third':
return createHogeC(data)
default:
return
}
}
- Create base interface
interface base {
id: number
name: string
}
But then honestly I didn't have idea
typecome from?