Reputation: 54
I am modeling a data structure in code to create an SDK. I have for example, two "types" of "letter" available. The abstract class letter
states that a character
property will always be present.
Further down there will be a function that will take in the letter
and print the character. The issue is, each class that extends letter
is its own type. How could this function expect to take in any class which extends letter
?
abstract class Letter {
abstract character: string
}
class A extends Letter {
character = "A"
}
class B extends Letter {
character = "B"
}
function printLetter(letter: Letter) {
console.log(letter.character)
}
printLetter(A) // see error
Error:
Argument of type 'typeof A' is not assignable to parameter of type 'Letter'.
Property 'character' is missing in type 'typeof A' but required in type 'Letter'.(2345)
Upvotes: 0
Views: 112
Reputation: 616
abstract class Letter {
abstract character: string
}
class A extends Letter {
character = "A"
}
class B extends Letter {
character = "B"
}
function printLetter(letter: Letter) {
console.log(letter.character)
}
const aInstance = new A()
printLetter(aInstance)
Upvotes: 1