diff --git a/packages/core/src/util/assert.ts b/packages/core/src/util/assert.ts index 165241ab1d..fc9ddc8412 100644 --- a/packages/core/src/util/assert.ts +++ b/packages/core/src/util/assert.ts @@ -14,68 +14,78 @@ import {stringify} from './stringify'; export function assertNumber(actual: any, msg: string) { if (typeof actual != 'number') { - throwError(msg); + throwError(msg, typeof actual, 'number', '==='); + } +} + +export function assertString(actual: any, msg: string) { + if (typeof actual != 'string') { + throwError(msg, actual === null ? 'null' : typeof actual, 'string', '==='); } } export function assertEqual(actual: T, expected: T, msg: string) { if (actual != expected) { - throwError(msg); + throwError(msg, actual, expected, '=='); } } export function assertNotEqual(actual: T, expected: T, msg: string) { if (actual == expected) { - throwError(msg); + throwError(msg, actual, expected, '!='); } } export function assertSame(actual: T, expected: T, msg: string) { if (actual !== expected) { - throwError(msg); + throwError(msg, actual, expected, '==='); } } export function assertNotSame(actual: T, expected: T, msg: string) { if (actual === expected) { - throwError(msg); + throwError(msg, actual, expected, '!=='); } } export function assertLessThan(actual: T, expected: T, msg: string) { if (actual >= expected) { - throwError(msg); + throwError(msg, actual, expected, '<'); } } export function assertLessThanOrEqual(actual: T, expected: T, msg: string) { if (actual > expected) { - throwError(msg); + throwError(msg, actual, expected, '<='); } } export function assertGreaterThan(actual: T, expected: T, msg: string) { if (actual <= expected) { - throwError(msg); + throwError(msg, actual, expected, '>'); } } export function assertNotDefined(actual: T, msg: string) { if (actual != null) { - throwError(msg); + throwError(msg, actual, null, '=='); } } export function assertDefined(actual: T, msg: string) { if (actual == null) { - throwError(msg); + throwError(msg, actual, null, '!='); } } -export function throwError(msg: string): never { +export function throwError(msg: string): never; +export function throwError(msg: string, actual: any, expected: any, comparison: string): never; +export function throwError(msg: string, actual?: any, expected?: any, comparison?: string): never { // tslint:disable-next-line debugger; // Left intentionally for better debugger experience. - throw new Error(`ASSERTION ERROR: ${msg}`); + throw new Error( + `ASSERTION ERROR: ${msg}` + + (comparison == null ? '' : ` [Expected=> ${expected} ${comparison} ${actual} <=Actual]`)); } export function assertDomNode(node: any) {