npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

zodselect

v1.0.9

Published

Type-safe selection utility built on top of Zod. Allows for strongly typed field selection, schema merging, and runtime refinement for frontend or backend use.

Downloads

16

Readme

ZodSelect

ZodSelect is a strongly typed selection utility library built on top of Zod (v4). It allows developers to create dynamic select statements with precise TypeScript inference — perfect for shaping responses in database queries, or APIs. ZodSelect ensures that both your types and runtime logic stay in sync — with no external dependencies.

  • ✅ Built on Zod v4
  • ✅ No external dependencies (except zod)
  • ✅ Fully type-safe field selections
  • ✅ Supports merging multiple Zod Selects
  • ✅ Generate Zod schemas from Zod Selects
  • ✅ Allows schema refinement and overrides

Install

pnpm add zod zodselect
# or
npm install zod zodselect

Example Schema

All examples use the following Zod schema:

const UserSchema = z.object({
	email: z.string(),
	firstName: z.string(),
	lastName: z.string(),
	address: z.object({
		street: z.string(),
		city: z.string(),
		state: z.string(),
		zip: z.string()
	})
});

🧠 Zod Select with Inferred Type

A single selection object returns a strongly typed result:

function find<T extends ZodSelect<typeof UserSchema>>(select: T): InferType<typeof UserSchema, T> {
	// ...Find Logic
}

const user = find({
	firstName: true,
	lastName: true,
	address: { street: true }
});
// user inferred as:
{
	firstName: string;
	lastName: string;
	address: { street: string };
}

🔀 Merging Multiple Zod Selects

You can combine multiple select statements and still get precise inference:

function find<T extends MultiSelect<typeof UserSchema>>(select: T): InferMergedType<typeof UserSchema, T> {
	// ...Find Logic
}

const user = find([
	{ lastName: true },
	{ firstName: true }
]);
// user inferred as:
{
	firstName: string;
	lastName: string;
}

🧹 Mix Zod Schemas with Zod Selects

You can mix plain field selections with Zod object schemas for flexible results:

function find<T extends MultiSelect<typeof UserSchema>>(select: T): InferMergedType<typeof UserSchema, T> {
	// ...Find Logic
}

const user = find([
	{ lastName: true },
	z.object({
		address: z.object({ state: z.string() })
	})
]);
// user inferred as:
{
	lastName: string;
	address: { state: string };
}

🛠️ Generate Zod Schemas from Zod Selects

Use refineSchema to turn a selection into a Zod schema:

const schema = refineSchema(UserSchema, {
	firstName: true,
	address: true
});
// schema.shape:
{
	firstName: z.ZodString;
	address: z.ZodObject<...>;
}

🧪 Refine Field Types Inline

Selections can refine field types inline using Zod methods:

const schema = refineSchema(UserSchema, {
	firstName: s => s.optional()
});
// schema.shape:
{
	firstName: z.ZodOptional<z.ZodString>;
}

🧬 Redefine Field Types

Selections can also redefine types entirely:

const schema = refineSchema(UserSchema, {
	firstName: z.number()
});
// schema.shape:
{
	firstName: z.ZodNumber;
}

🧵 Putting it all together

Combining all methods and types together you can build powerful, type-safe selection and inference using the zod schemas you already have.

function find<T extends MultiSelect<typeof UserSchema>>(select: T): InferMergedType<typeof UserSchema, T>[] {
	const selectArr = Array.isArray(select) ? select : [select];
	const sel: Record<string, 1> = {};

	for (let schema of selectArr) {
		if (!(schema instanceof ZodType)) {
			schema = refineSchema(UserSchema, schema);
			continue;
		}

		// Crawl each zod schema building the select statement
	}

	// Find Logic
}

function getUsersFromTexas<T extends MultiSelect<typeof UserSchema>>(
	sel: T
): InferMergedType<typeof UserSchema, MergeSelect<typeof UserSchema, { address: boolean }, T>>[] {
	const users = find(
		// Use the merge select function to combined a ZodSelect with a MultiSelect
		mergeSelect(UserSchema, { address: true }, sel)
	);

	return users.filter((user) =>
		// At this point the user object has address and whatever fields are specified in the
		// passed in select but VS code doesn't know what fields are going to be passed in at
		// runtime. It does know that address was selected inside this function so it is
		// strongly typed with a fully selected address object
		user.address.state === 'TX'
	);
}

const users = getUsersFromTexas({
	firstName: true,
	lastName: true
});

// At this point we know what fields we selected and the extra fields added by the
// getUsersFromTexas function
// user inferred as:
{
	firstName: string;
	lastName: string;
	address: {
		street: string;
		city: string;
		state: string;
		zip: string;
	};
}[]