type-fest README

项目的 README,来自 github.com/sindresorhus/type-fest。合并为一个 markdown 文件:README.md。



type-fest

A collection of essential TypeScript types





npm dependents npm downloads

Many of the types here should have been built-in. You can help by suggesting some of them to the TypeScript project.

Either add this package as a dependency or copy-paste the needed types. No credit required. 👌

PR welcome for additional commonly needed types and docs improvements. Read the contributing guidelines first.

Help wanted with reviewing proposals and pull requests.

Install

npm install type-fest

Requires TypeScript >=6, ESM, and {strict: true} in your tsconfig.

{exactOptionalPropertyTypes: true} is required. Disabling it is unsupported.

[!NOTE] This readme shows the current development version. For docs about the latest version, see the npm page.

You may also like my ts-extras package which provides runtime functions for some of these types.

Usage

import type {Except} from 'type-fest';

type Foo = {
	unicorn: string;
	rainbow: boolean;
};

type FooWithoutRainbow = Except<Foo, 'rainbow'>;
//=> {unicorn: string}

Try it in the TypeScript Playground

API

Click the type names for complete docs.

Basic

Utilities

Type Guard

JSON

Structured clone

Function

Async

String

Array

Numeric

Change case

Miscellaneous

Improved built-in

Declined types

If we decline a type addition, we will make sure to document the better solution here.

Alternative type names

If you know one of our types by a different name, add it here for discovery.

Tips

Extending existing types

“Cannot be named” errors in declaration files

When TypeScript generates declaration files, it writes out the inferred type of each export. If that type uses a type from this package, it can refer to an internal helper type or symbol, and you get an error such as TS2742, TS2883, TS4023, or TS4058 (“The inferred type of 'x' cannot be named without a reference to…”). Internal types and symbols are not part of the public API, so they cannot be imported. This is a TypeScript limitation. Add an explicit type annotation to the export instead:

import type {EmptyObject, ReadonlyDeep} from 'type-fest';

type Config = {
	server: {
		port: number;
	};
};

declare function freeze<Value>(value: Value): ReadonlyDeep<Value>;

export const config: ReadonlyDeep<Config> = freeze({server: {port: 8080}});

export function copy(value: EmptyObject): EmptyObject {
	return {...value};
}

Also make sure type-fest is a direct dependency of the package that generates the declaration files.

Built-in types

There are many advanced types most users don't know about.

You can find some examples in the TypeScript docs.

Maintainers

License

SPDX-License-Identifier: (MIT OR CC0-1.0)

type-fest (sindresorhus/type-fest)

正在搜索数据库…

这是干什么的?type-fest 是一套实用的 TypeScript 类型合集——作者认为这些辅助类型本应内置在语言里,涵盖对象、联合类型、字符串、数组、元组和函数。
它是运行时库,还是只有类型定义?只有类型——也就是编译器在构建阶段使用的类型定义,不会在你的应用里执行任何代码。如果需要运行时函数,作者另外发布了一个配套包 ts-extras。
我怎么把它加到项目里?用 npm install type-fest 把它装成依赖,然后导入你需要的类型——或者干脆不装这个包,直接把你想要的那几条定义复制粘贴过去也行。
我的项目对 TypeScript 配置有要求吗?有——需要 TypeScript 5.9 或更新版本、ESM,以及 tsconfig 里开启严格模式({strict: true});较旧的或未开严格模式的代码库用不了它。另外,GitHub 上的 README 写的是尚未发布的开发版内容,正式版本的文档请看 npm。
它都包含哪些类型的类型?远超一百个,分三组:基础类(Primitive、Class、TypedArray)、工具类(Except、Merge、LiteralUnion、PartialDeep、Get……)和类型守卫类(If、IsLiteral、IsNumericLiteral)。
哪些类型值得先了解?Except 可以从对象类型中剔除指定的键,Merge 可以合并两个类型,LiteralUnion 能让字面量联合类型在 IDE 里保留自动补全,Get 则像 Lodash 的 .get() 那样取嵌套属性的类型。
如果我把类型复制粘贴到自己的代码里,需要署名作者吗?不用——type-fest 采用 CC0-1.0 许可证,实际上属于公共领域,README 也说明复制粘贴所需的类型无需署名。
它能跟上 TypeScript 的新版本吗?应该可以:这个仓库的 CI 里包含一个 canary 工作流,会用尚未发布的 TypeScript 构建做测试;README 还鼓励大家把值得纳入语言本身的类型建议提交给 TypeScript 官方项目。
大小
5.3 MB
星标
17.5K
时长
7年
提交 · 90 天