Reputation: 2222
I'm looking for something similar to the implict constructor in other languages.
I want to assign a MyNumber
variable via a single literal number such as:
class MyNumber {
value:number
constructor (value:number) {
this.value = value
}
}
const mynumner : MyNumber = 1
And I got an error: Type 'number' is not assignable to type 'MyNumber'.ts(2322)
Can I make this type to be number assignable
?
Upvotes: 0
Views: 63
Reputation: 12247
No, you can't.
TypeScript does not (currently) support no-arg class constructors.
Upvotes: 2