TypeScript

嵌套对象的keyof

需求 interface User { parent: { name: string; }; age: number; name: string; } type Column = { key: keyof User; }; const columns: Column[] = [ { key: 'name' }, { key: 'name1' }, { key:'parent.name' } ]; 解决办法 type Paths<T> = T extends object ? { [K in keyof T]: `${Exclude<K, symbol>}${"" | `.${Paths<T[K]>}`}` }[keyof T] : never type Column = { key: Paths<User>; }; 除了自己写Paths类型外,还可以使用现成的库,比如type-fest中的Path类型。

7月 22, 2025

Keyof for Nested Objects

Problem interface User { parent: { name: string; }; age: number; name: string; } type Column = { key: keyof User; }; const columns: Column[] = [ { key: 'name' }, { key: 'name1' }, { key:'parent.name' } ]; Solution type Paths<T> = T extends object ? { [K in keyof T]: `${Exclude<K, symbol>}${"" | `.${Paths<T[K]>}`}` }[keyof T] : never type Column = { key: Paths<User>; }; In addition to writing your own Paths type, you can also use existing libraries, such as the Path type from type-fest.

7月 22, 2025

第三方包中全局类型定义不work问题

本文介绍关于第三方包中全局类型定义不work问题,包括使用场景、实现细节等,以提高关于第三方包中全局类型定义不work问题的效率。

4月 5, 2022

Global Type Definition Issues in Third-party Packages

When global types in third-party packages don't apply, here’s how to wire them up in TS or JS projects and the ideal @types approach.

4月 5, 2022

前端项目中增加全局常量设置

本文介绍关于前端项目中增加全局常量设置,包括使用场景、实现细节等,以提高关于前端项目中增加全局常量设置的效率。

1月 25, 2022

Add Global Constants to a Frontend Project

Expose config from package.json into the built app via webpack DefinePlugin, with typing in TS and pitfalls to avoid.

1月 25, 2022

给开源项目贡献TS类型声明

本文介绍关于给开源项目贡献TS类型声明,包括使用场景、实现细节等,以提高关于给开源项目贡献TS类型声明的效率。

9月 12, 2021

Contributing TS Type Declarations to Open Source Projects

How to create, package, and reference TypeScript declaration files when contributing typings to open-source projects.

9月 12, 2021

TypeScript中一些特殊类型

本文是作者对TypeScript中一些特殊类型的介绍,包括TypeScript中一些特殊类型的优势、实现细节、相关资料等,这些步骤可以帮助作者提高TypeScript中一些特殊类型的效率。

9月 5, 2021

Lesser-Known TypeScript Types

Covers `never`, `ArrayLike`, and `PromiseLike`, when to use them, and links to further reading.

9月 5, 2021