Skip to content

Instantly share code, notes, and snippets.

@shahanahmed86
Last active April 13, 2026 06:46
Show Gist options
  • Select an option

  • Save shahanahmed86/3d0625dc6459982808ed3d3ff8d2839d to your computer and use it in GitHub Desktop.

Select an option

Save shahanahmed86/3d0625dc6459982808ed3d3ff8d2839d to your computer and use it in GitHub Desktop.
import { z } from 'zod'; // "zod": "^3.23.8"
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Listener<T extends any[] = any[]> = (...args: T) => void;
// With a simple object records
class EventEmitter<T extends Record<string, unknown[]>> {
private listeners: { [K in keyof T]?: Set<Listener<T[K]>> } = {};
on<K extends keyof T>(eventName: K, listener: Listener<T[K]>): void {
const listeners = this.listeners[eventName] ?? new Set();
listeners.add(listener);
this.listeners[eventName] = listeners;
}
emit<K extends keyof T>(eventName: K, ...args: T[K]): void {
const listeners = this.listeners[eventName] ?? new Set();
for (const listener of listeners) listener(...args);
}
}
// With a Map records
// class EventEmitter<T extends Record<string, unknown[]>> {
// private listeners: Map<keyof T, Set<Listener>> = new Map();
// on<K extends keyof T>(eventName: K, listener: Listener<T[K]>): void {
// const listeners = this.listeners.get(eventName) ?? new Set();
// listeners.add(listener);
// this.listeners.set(eventName, listeners);
// }
// emit<K extends keyof T>(eventName: K, ...args: T[K]): void {
// const listeners = this.listeners.get(eventName) ?? new Set();
// for (const listener of listeners) listener(...args);
// }
// }
type Login = { id: string; username: string };
type SignUp = Omit<Login, 'id'> & { password: string };
type AuthEvent = {
login: [user: Login];
signup: [user: SignUp];
logout: [username: string];
};
const authEvent = new EventEmitter<AuthEvent>();
authEvent.on('login', (user) => {
console.log(`The USER, id = ${user.id}, username = ${user.username}`);
});
authEvent.on('signup', (user) => {
console.log(`The USER, username = ${user.username}, password = ${user.password}`);
});
authEvent.on('logout', (username) => {
console.log(`The username ${username} has signed out`);
});
authEvent.emit('login', { id: '123', username: 'shahanahmed86' });
authEvent.emit('signup', { username: 'shahanahmed86', password: '123Abc456%' });
authEvent.emit('logout', 'shahanahmed86');
const User = z.object({
login: z.string(),
id: z.number(),
node_id: z.string(),
avatar_url: z.string().url(),
gravatar_id: z.string(),
url: z.string().url(),
html_url: z.string().url(),
followers_url: z.string().url(),
following_url: z.string().url(),
gists_url: z.string().url(),
starred_url: z.string().url(),
subscriptions_url: z.string().url(),
organizations_url: z.string().url(),
repos_url: z.string().url(),
events_url: z.string().url(),
received_events_url: z.string().url(),
type: z.string(),
site_admin: z.boolean(),
name: z.string(),
company: z.string().nullable(),
blog: z.string(),
location: z.string(),
email: z.string().email().nullable(),
hireable: z.boolean().nullable(),
bio: z.string().nullable(),
twitter_username: z.string().nullable(),
public_repos: z.number(),
public_gists: z.number(),
followers: z.number(),
following: z.number(),
created_at: z.string().datetime(),
updated_at: z.string().datetime(),
});
type User = z.infer<typeof User>;
(async () => {
console.time('github');
const user = await makeFetch('https://api.github.com/users/{username}', User);
console.timeEnd('github');
console.log(`The data related to ${user.login} has successfully been fetched`);
console.dir(user, { depth: 1 });
})();
async function makeFetch<T>(url: string, schema: z.ZodType<T>): Promise<T> {
return fetch(url)
.then((res) => res.json())
.then(schema.parse);
}
// to make union types to access dynamic string also
type Model = 'compose' | 'sonnet' | 'opus' | 'mythos' | (string & NonNullable<unknown>);
const model = 'compose-2' satisfies Model;
console.log(`selected model is ${model}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment