This is super interesting to me, @solemn path .
declare const _never: unique symbol;
const reservedCodes = {} as { [_never]: never };
type Combine<T extends PropertyKey, E> = {
[K in T]: K;
} & E;
function registerError<A extends PropertyKey, B>(
a: A extends keyof B ? never : A,
b: B
): asserts b is Combine<A, B> {}
registerError(1111, reservedCodes);
registerError(2222, reservedCodes);
registerError(1111, reservedCodes);
Simpler syntax though the error is more cryptic.
Essentially asserts forces typescript to update it's in memory version of the value [read type]. Like @ashen pine said, I'm curious if this works across multiple files. I imagine that any assertion using the same codes object (reservedCodes) would update the type, regardless of where it occurred?
ALSO, is it possible to remove the second argument from the function call? I realize asserts only works with function arguments but is it possible to do some more TS wizardry and make reservedCodes the default value (IE don't require the user to pass it)?