| 12 | * Number type |
| 13 | */ |
| 14 | export class NumberType implements Type<number> { |
| 15 | readonly name = 'number'; |
| 16 | |
| 17 | isInstance(value: any) { |
| 18 | return value == null || (!isNaN(value) && typeof value === 'number'); |
| 19 | } |
| 20 | |
| 21 | isCoercible(value: any): boolean { |
| 22 | return value == null || !isNaN(Number(value)); |
| 23 | } |
| 24 | |
| 25 | defaultValue() { |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | coerce(value: any) { |
| 30 | if (value == null) return value; |
| 31 | const n = Number(value); |
| 32 | if (isNaN(n)) { |
| 33 | const msg = util.format('Invalid %s: %j', this.name, value); |
| 34 | throw new TypeError(msg); |
| 35 | } |
| 36 | return n; |
| 37 | } |
| 38 | |
| 39 | serialize(value: number | null | undefined) { |
| 40 | return value; |
| 41 | } |
| 42 | } |
nothing calls this directly
no outgoing calls
no test coverage detected