ArgumentError.ts (3254B)
1 export interface AnyConstructor { 2 new(args: any): any; 3 } 4 5 export type ArgumentType = 6 | "undefined" 7 | "null" 8 | "boolean" 9 | "number" 10 | "string" 11 | "object" 12 | "Array" 13 | "ArrayBuffer" 14 | "ArrayBufferView" 15 | AnyConstructor; 16 17 export class ArgumentError extends TypeError { 18 19 public static readonly NAME = "ArgumentError"; 20 21 public static isType(value: any, type: "undefined"): value is undefined; 22 public static isType(value: any, type: "null"): value is null; 23 public static isType(value: any, type: "boolean"): value is boolean; 24 public static isType(value: any, type: "number"): value is number; 25 public static isType(value: any, type: "object"): value is object; 26 public static isType(value: any, type: "string"): value is string; 27 public static isType(value: any, type: "Array"): value is any[]; 28 public static isType(value: any, type: "ArrayBuffer"): value is ArrayBuffer; 29 public static isType(value: any, type: "ArrayBufferView"): value is ArrayBufferView; 30 public static isType<T>(value: any, type: new (...args: any[]) => T): value is T; 31 // @internal 32 public static isType(value: any, type: ArgumentType): boolean; 33 public static isType(value: any, type: ArgumentType): boolean { 34 if (typeof type === "string") { 35 if (type === "Array" && Array.isArray(value)) { 36 return true; 37 } else if (type === "ArrayBuffer" && value instanceof ArrayBuffer) { 38 return true; 39 } else if (type === "ArrayBufferView" && ArrayBuffer.isView(value)) { 40 return true; 41 } else if (typeof value === type) { 42 return true; 43 } 44 } else if (value instanceof type) { 45 return true; 46 } 47 48 return false; 49 } 50 51 public static assert(value: any, name: string, type: "undefined"): asserts value is undefined; 52 public static assert(value: any, name: string, type: "null"): asserts value is null; 53 public static assert(value: any, name: string, type: "boolean"): asserts value is boolean; 54 public static assert(value: any, name: string, type: "number"): asserts value is number; 55 public static assert(value: any, name: string, type: "object"): asserts value is { [key: string]: any; }; 56 public static assert(value: any, name: string, type: "string"): asserts value is string; 57 public static assert(value: any, name: string, type: "Array"): asserts value is any[]; 58 public static assert(value: any, name: string, type: "ArrayBuffer"): asserts value is ArrayBuffer; 59 public static assert(value: any, name: string, type: "ArrayBufferView"): asserts value is ArrayBufferView; 60 public static assert<T>(value: any, name: string, type: new (...args: any[]) => T): asserts value is T; 61 public static assert(value: any, name: string, type: ArgumentType, ...types: ArgumentType[]): void; 62 public static assert(value: any, name: string, ...types: ArgumentType[]): void { 63 for (const type of types) { 64 if (this.isType(value, type)) { 65 return; 66 } 67 } 68 69 const typeNames = types.map(o => o instanceof Function && "name" in o ? o.name : `${o}`); 70 throw new ArgumentError(`Parameter '${name}' is not of type ${typeNames.length > 1 ? `(${typeNames.join(" or ")})` : typeNames[0]}`); 71 } 72 73 public override name: typeof ArgumentError.NAME = ArgumentError.NAME; 74 75 }