

PLAY · SNIPPET
eww, a vimmer!
I've been getting (back) into vim lately, to the detriment of my productivity in both personal and work projects. A short-term struggle for a long-term payoff, if I'm to believe the vim gods.
I'm taking baby steps though - LazyVim's configuration over NeoVim works for me, for now. And hey, I didn't touch my mouse ONCE throughout the 3 or 4 minutes I spent writing this braindead discriminated union in TypeScript.
The code, if you care
It's just an overly verbose and useless discriminated union with a random namespace in there to make things spicy. I like to keep people on their toes.
const UserKind = {
Admin: 'admin',
Visitor: 'visitor',
} as const;
type BaseUser = {
name: string;
}
type Admin = BaseUser & {
kind: typeof UserKind.Admin;
}
type Visitor = BaseUser & {
kind: typeof UserKind.Visitor;
}
type User = Visitor | Admin;
namespace Api {
export const printName = (username: string, isVisitor?: boolean): void => {
if (isVisitor) {
throw new Error(`Welcome, ${username}, but you're a visitor. No printing for you.`);
}
console.log(`Welcome, ${username}. You're the goat.`);
}
}
const makeUser = (name: string, grantRights?: boolean): User => {
return {
name,
kind: grantRights ? UserKind.Admin : UserKind.Visitor,
};
}
const printUser = (user: User): void => {
Api.printName(user.name, user.kind === UserKind.Visitor)
}
// ------- EXAMPLES --------
const ADMIN = makeUser("theGoat", true);
const STANDARD_USER = makeUser("peasant");
const USERS = [ADMIN, STANDARD_USER]