jayatubi
jayatubi

Reputation: 2222

Can I make my custom class to be assignable from number in TypeScript?

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

Answers (1)

msanford
msanford

Reputation: 12247

No, you can't.

TypeScript does not (currently) support no-arg class constructors.

Upvotes: 2

Related Questions