tridactyl/compiler/types/NumberType.ts

28 lines
649 B
TypeScript
Raw Normal View History

import { Type } from "./Type"
export class NumberType implements Type {
public kind = "number"
public constructor(public isDotDotDot = false, public isQuestion = false) {}
public toConstructor() {
return `new NumberType(${this.isDotDotDot}, ${this.isQuestion})`
}
public toString() {
return this.kind
}
public convert(argument) {
2019-04-05 13:16:04 +02:00
let n = parseInt(argument, 10)
if (!Number.isNaN(n)) {
return n
}
n = parseFloat(argument)
if (!Number.isNaN(n)) {
return n
}
throw new Error(`Can't convert to number: ${argument}`)
}
}