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

ts-type-object-entries

v1.0.10

Published

為 Object.entries 提供更好的 TypeScript 類型推論 | Better TypeScript types for Object.entries

Readme

ts-type-object-entries

為 Object.entries 提供更好的 TypeScript 類型推論

Better TypeScript types for Object.entries


問題:Object.entries 的類型寬化

標準的 JavaScript Object.entries() 在 TypeScript 中的回傳類型是 [string, any][],導致類型資訊丢失:

const obj = { a: 1, b: 2 };

// ❌ 標準 Object.entries - 類型丢失
Object.entries(obj);
// 回傳: [string, number][]
// 鍵名從 "a" | "b" 變成了 string

解決方案

ts-type-object-entries 提供更精確的鍵值對回傳類型:

import { tsObjectEntries } from 'ts-type-object-entries';

const obj = { a: 1, b: 2 } as const;
const entries = tsObjectEntries(obj);
// ✅ 回傳: readonly ["a", 1][] | readonly ["b", 2][]

功能特點

  • 支援物件類型的精確鍵值對回傳
  • Support precise key-value pair return for object types
  • 保留 readonly 陣列類型
  • Preserve readonly array types
  • 完整的 TypeScript 類型安全
  • Complete TypeScript type safety

安裝

yarn add ts-type-object-entries
yarn-tool add ts-type-object-entries
yt add ts-type-object-entries

使用範例

基本使用

import { tsObjectEntries } from 'ts-type-object-entries';

const obj = { a: 1, b: 2 } as const;
const entries = tsObjectEntries(obj);
// type: readonly ("a" | "b")[][]
// [['a', 1], ['b', 2]]

介面類型

import { tsObjectEntries } from 'ts-type-object-entries';

interface IUser {
    name: string;
    age: number;
}

const user: IUser = { name: 'John', age: 30 };
const userEntries = tsObjectEntries(user);
// [key: string, value: string | number][]

在迴圈中使用

import { tsObjectEntries } from 'ts-type-object-entries';

const config = {
    apiUrl: "https://api.example.com",
    timeout: 5000,
    retries: 3,
} as const;

for (const [key, value] of tsObjectEntries(config)) {
    console.log(`${key}: ${value}`);
    // key 的類型精確為 "apiUrl" | "timeout" | "retries"
}

比較

| 特性 | 標準 Object.entries | tsObjectEntries | |------|----------------------|-------------------| | 鍵名類型 | string | "key1" \| "key2" \| ... | | 值類型 | any | 具體類型 | | readonly 支援 | 部分 | 完整 | | as const 支援 | 有限 | 完整 |


核心概念

在類型系統中,保留越多精確資訊,類型安全性就越高。

這個套件體現了這個概念:

  • 保留鍵名資訊:不將鍵名寬化為 string
  • 保留值類型:使用具體類型而非 any
  • 完整 readonly 支援:正確處理 as const