classes with static check
methods. wanted to find the
matching one and return to it in wrap
method.
this architecture perfectly suits for my need of OOP-ifying.
here is the way that I used to do it in JavaScript:
class A {
static check = arg => typeof arg == "number"
constructor(arg) { this.value = arg}
log = () => console.log("A/", this.value)
}
class B {
static check = arg => typeof arg == "string"
constructor(arg) { this.note = arg }
log = () => console.log("B/", this.note)
}
function wrap(arg, classes) {
for (const Class of classes)
if (Class.check(arg))
return new Class(arg)
}
const classes = [A, B]
wrap("adaptive", classes).log()
wrap(999, classes).log()
lately, I thought maybe TypeScript could help me with that. what
I expected was that the wrap
call would indicate the return
value (since it’s all about types with no logic)
unfortunately, it was not the case. the closest I could get was that:
type PayloadA = number
type PayloadB = string
type PayloadGeneric = PayloadA | PayloadB
class A {
static check(arg:PayloadGeneric): arg is PayloadA {
return typeof arg == "number"
}
value: number
constructor(arg: PayloadA) { this.value = arg}
log = () => console.log("A/", this.value)
}
class B {
static check(arg:PayloadGeneric): arg is PayloadB {
return typeof arg == "string"
}
note: string
constructor(arg: PayloadB) { this.note = arg}
log = () => console.log("B/", this.note)
}
function wrap(arg: PayloadGeneric) {
if (A.check(arg)) { // understands that arg is number
return new A(arg) // and that arg is suitable for PayloadA
}
if (B.check(arg)) { // understands that arg is string
return new B(arg) // and that arg is suitable for PayloadB
}
}
const instanceA = wrap(999) // but here it doesn't understand the type
const instanceB = wrap("adaptive") // neither here
(I know PayloadA
and PayloadB
may look silly since they’re just
number
and string
but the whole point is about having custom interfaces
and predicates. number and string is just for the example and wrapping them
into a type demonstrates my purpouse better)
look wrap
can only accept PayloadA
or PayloadB
. inside, we have 2 if
s.
one checks for PayloadA
and understands and returns, the other checks
for PayloadB
and again understands well and returns. but TypeScript
doesn’t stop there and continues reading the rest, like it may get executed.
even thinks it may return undefined
since I’m not handling the default
return, but logically, I do – I only accept A or B and handled both. so,
- how can I make ts understand that all the cases are handled
- and how to use arrays instead of hard coded
if
s - is there a better way for TypeScript? the problem might be my js brain
and please don’t tell me “just put return type A | B
” I don’t wanna hardcode
cuz the given list can change. I may put classes that none of their check
method
matches, in this case I’d like to see that the return type is going to be undefined
.
or I put one that matches, then I’d like to see what the return type is gonna be.
thanks in advance