Kevin Powell - CommunityKP-C
Kevin Powell - Community8mo ago
40 replies
Ganesh

typescript narrow type on throw

I have this code here
const SALT_ROUNDS = process.env.SALT_ROUNDS;

if (!SALT_ROUNDS) throw new Error("salt rounds not specified for bcrypt");

export { SALT_ROUNDS}

but it doesn't narrow salt rounds type for export.

function assertTruthy (
    param: unknown,
    envName: string
): asserts param is string {
    if (!param) throw new Error(`${envName} is not provided`);
    else return;
}

assertTruthy(SALT_ROUNDS, "SALT_ROUNDS");


I tried this but it also doesn't work. Do i have to reassign a value or is there another way
Was this page helpful?