Skip to content

Commit 017ca7b

Browse files
committed
Define type-assertion functions.
1 parent 376a85e commit 017ca7b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

lib/ecmascript.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,31 @@ const BUILTIN_CALENDAR_IDS = [
120120
'gregory'
121121
];
122122

123+
/*
124+
* uncheckedAssertNarrowedType forces TypeScript to change the type of the argment to the one given in
125+
* the type parameter. This should only be used to help TS understand when variables change types,
126+
* but TS can't or won't infer this automatically. They should be used sparingly, because
127+
* if used incorrectly can lead to difficult-to-diagnose problems.
128+
*/
129+
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function */
130+
export function uncheckedAssertNarrowedType<T = unknown>(
131+
arg: unknown,
132+
justification: string
133+
): asserts arg is T extends typeof arg ? T : never {}
134+
/* eslint-enable */
135+
/**
136+
* In debug builds, this function verifies that the given argument "exists" (is not
137+
* null or undefined). This function becomes a no-op in the final bundles distributed via NPM.
138+
* @param arg
139+
*/
140+
function assertExists<A>(arg: A): asserts arg is NonNullable<A> {
141+
if (DEBUG) {
142+
if (arg != null) {
143+
throw new Error('Expected arg to be set.');
144+
}
145+
}
146+
}
147+
123148
function IsInteger(value: unknown): value is number {
124149
if (typeof value !== 'number' || !NumberIsFinite(value)) return false;
125150
const abs = MathAbs(value);

0 commit comments

Comments
 (0)