type-fest
TypeScript utility types library. Pure type-level programming—no runtime code.
Read .github/contributing.md for complete guidelines.
Commands
npm test # MUST pass before committing (runs all below)
npm run test:tsc # TypeScript compiler
npm run test:nz # Type tests (nz)
npm run test:xo # Linter
File Structure
source/type-name.d.ts → Type definition (.d.ts REQUIRED)
test-d/type-name.ts → Tests (nz)
index.d.ts → Export (MUST add, lint enforces)
readme.md → API docs (add to category)
source/internal/ → Shared helpers (not exported)
Code Patterns
// ✅ CORRECT
import type {IsNever} from './is-never.d.ts'; // Include .d.ts
export type MyType<Value extends string> = ... // Descriptive names (not T/U)
export {}; // REQUIRED at end
// ❌ WRONG
import type {IsNever} from './is-never'; // Missing .d.ts
export type MyType<T> = ...; // Single-letter
/**
Creates a tuple type of specified length with elements of specified type.
Use-cases:
- Define fixed-length arrays with specific types
@example
```
import type {TupleOf} from 'type-fest';
type RGB = TupleOf<3, number>; //=> [number, number, number]
```
@category Array
*/
export type TupleOf<Length extends number, Fill = unknown> = ...;
export {};
Type params: Value, Target, Item, Length, Element, Key (not T, U, V, K)
Docs: No * prefix. First line → readme. Include use-cases, examples, @category.
Link other types with {@link Simplify}, and import type the linked type with a // For `@link` comment so the link resolves in editors. The type-fest/jsdoc-link-imports lint rule enforces this, and npx xo --fix adds the imports, except in a file that has no imports and starts with a line comment (for example a triple-slash directive). Add the import by hand there.
Testing
Only exactOptionalPropertyTypes: true is supported. Do not add fixes, tests, compiler configurations, or compatibility logic for exactOptionalPropertyTypes: false.
ALWAYS test edge cases (any, never, unknown).
expectTypeOf<MyType<'foo'>>().toBe<string>(); // Basic
expectTypeOf<MyType<any>>().toBe<any>(); // any/never/unknown
expectTypeOf<{wrong: true}>().not.toBeAssignableTo<MyType<'foo'>>(); // Negative
// @ts-expect-error // Should error
type Invalid = MyType<number>;
Study: source/tuple-of.d.ts, is-equal.d.ts, literal-union.d.ts, internal/*.d.ts
Workflow
- Research - Study
source/similar types + online research - Define -
source/type-name.d.tswith docs - Test -
test-d/type-name.tswith edge cases - Export - Add to
index.d.ts+ readme.md - Verify -
npm testmust pass - Commit - See Commit titles below
Commit titles
- New type:
Add `TypeName` type. Several new types:Add `MapKey` and `MapValue` types. - Any change to existing types (fix, docs, tests, readme entry, alternative name) MUST start with the type names and a colon:
`TypeName`: Fix description. - Several existing types: list them all, separated by commas:
`TypeA`, `TypeB`, `TypeC`: Fix docs. - Never start the title with "Add" when you change an existing type, even when you add something to it. Write
`AllUnionFields`: Document `MergeUnion` as an alternative name, notAdd `MergeUnion` as an alternative name for `AllUnionFields`. - Only changes that are not about specific types (tooling, dependencies, general docs) use a plain title, like
Update dependencies.
Critical Rules
- Import paths MUST include
.d.tsextension - Files MUST end with
export {}; - Types MUST be exported from
index.d.ts - Type params MUST use descriptive names (not
T/U) - MUST test
any,never,unknownedge cases - First doc line MUST be concise (goes in readme)
- Use realistic examples with
//=>comments - Include
@categorytags - Fix lint issues, don't disable rules
Type Programming
- Conditionals:
T extends U ? X : Y - Recursion:
type Loop<T, Acc> = ... Loop<...> ... - Extract:
infer Item - Distribute:
T extends any ? ... : never - Count via tuple
['length'] - Union distribution is tricky—test it
- Predicate results are an API contract: Use
true/falsefor definite whole-type classification (IsNull<any>andIsPrimitive<any>arefalse). Usebooleanwhen preserving uncertainty or mixed distributed results is intentional. Explicitly document and testany,never,unknown, and union behavior. - A boolean predicate returns
falsefornever(likeIf,And,Or, andNot, which readneverasfalse), and for aneverin any input of a multi-input predicate. Guard withIsAny<T> extends true ? <any result> : IsNever<T> extends true ? false : <logic>. Checkanyfirst: anever-first guard gives wrong results in generic code that wraps the result and infers from it, like[IsFloat<T>] extends infer R extends [boolean] ? ([true] extends R ? 'yes' : 'no') : never. Test that pattern with a matching value. - Key predicates read an
anyobject likeRecord<PropertyKey, any>, an object that can hold any key, because TypeScript does:keyof anyisPropertyKey, and bothconst value: any = {}andvalue.foo = 1compile. This one rule gives the four results:HasWritableKeys<any>istrue(any property can be written), andHasRequiredKeys<any>,HasOptionalKeys<any>, andHasReadonlyKeys<any>arefalse({}is accepted, and no key is optional or readonly).WritableKeysOf<any>andRequiredKeysOf<any>arePropertyKey, as for an index signature.Is*KeyOf<any, Key>answers likeIs*KeyOf<Record<PropertyKey, any>, Key>: a literal key is optional, not required, and writable, and ananykey givesbooleanwhere the answer depends on the key. TheIsNull<any>rule does not apply: it classifies the whole type, while these types answer questions about its keys. Do not returnfalsefor everyanyinput. - Path autocomplete: use
LiteralUnion<Paths<T, {maxRecursionDepth: 2}>, string | number>, never full-depthPaths<T>. DeeperPathsin a constraint can take minutes on large union-heavy types (#954), andnumberkeeps a genericPaths<T>valid.
Philosophy
- Correctness > cleverness
- Real problems only
- Docs teach how, not what
- Edge cases mandatory
- One concept per PR
- Descriptive names > brevity
- No unrelated changes
- Do not add entries to the readme "Declined types" section. The issue or pull request thread keeps the reason.
Unsupported
These cases cost performance and consistency for few users. Do not add handling for them. Document them as limitations instead.
- Key matching beyond TypeScript's own rules (
keyof,Pick,Omit): enum fields selected by plain strings, noncanonical numeric strings ('00','1e3'), and reading every union member at the keys of all members. The one supported alias is0vs'0'. UseMatchingKeyfromsource/matching-key.d.tsfor it.KeySelectoralso maps enum members to their values, and some existing types match more (for example throughKeyAliasesorNormalizedKeys). Do not grow that or extend it to other types, and do not add another matcher. - A union of option values (for example
{recurseIntoArrays: boolean}), a union of option objects, or aneveroption value. Do not compute a union of strategies. The result uses one strategy, usually the default. - Extreme edge cases that real code does not hit, such as exponent-notation numbers (
1e21), huge lengths, or fractional indexes. Handling one usually breaks another.
Troubleshooting
- Tests fail? Check
.d.tsimports,export {};, edge cases (any,never,unknown) - Lint errors? Add to
index.d.ts, fix issues (don't disable rules)