Is it possible to create a TypeScript assertion function that asserts the type of something other than the arguments passed to it – a global, for instance:
// Pseudo-code
function assertIsSubtleCryptoSupported(
): asserts globalThis.crypto.subtle is InstanceType<SubtleCrypto> {
if (globalThis.crypto?.subtle === undefined) {
throw new Error('...');
}
}
globalThis.crypto.subtle; // Type error
assertIsSubtleCryptoSupported();
globalThis.crypto.subtle; // OK
function assert(global: unknown): asserts global is {crypto: {subtle: type}} {}and usingassert(global)asserts this is ⋯). There's no facility to affect other values in other scopes. Does that fully address the question? If so I could write up an answer explaining; if not, what am I missing?