| 12 | * Array type, such as string[] |
| 13 | */ |
| 14 | export class ArrayType<T> implements Type<T[]> { |
| 15 | constructor(public itemType: Type<T>) {} |
| 16 | |
| 17 | readonly name = 'array'; |
| 18 | |
| 19 | isInstance(value: any) { |
| 20 | if (value == null) return true; |
| 21 | if (!Array.isArray(value)) { |
| 22 | return false; |
| 23 | } |
| 24 | const list = value as (T | null | undefined)[]; |
| 25 | return list.every(i => this.itemType.isInstance(i)); |
| 26 | } |
| 27 | |
| 28 | isCoercible(value: any): boolean { |
| 29 | if (value == null) return true; |
| 30 | if (!Array.isArray(value)) { |
| 31 | return false; |
| 32 | } |
| 33 | return value.every(i => this.itemType.isCoercible(i)); |
| 34 | } |
| 35 | |
| 36 | defaultValue(): T[] { |
| 37 | return []; |
| 38 | } |
| 39 | |
| 40 | coerce(value: any) { |
| 41 | if (value == null) return value; |
| 42 | if (!Array.isArray(value)) { |
| 43 | const msg = util.format('Invalid %s: %j', this.name, value); |
| 44 | throw new TypeError(msg); |
| 45 | } |
| 46 | return value.map(i => this.itemType.coerce(i)); |
| 47 | } |
| 48 | |
| 49 | serialize(value: T[] | null | undefined) { |
| 50 | if (value == null) return value; |
| 51 | return value.map(i => this.itemType.serialize(i)); |
| 52 | } |
| 53 | } |
nothing calls this directly
no outgoing calls
no test coverage detected