Repository: sindresorhus/type-fest
Stars: 17067
CLAUDE.md
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:tsd # Type tests (tsd)
npm run test:xo # LinterFile Structure
source/type-name.d.ts β Type definition (.d.ts REQUIRED)
test-d/type-name.ts β Tests (tsd)
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.
Testing
ALWAYS test edge cases (any, never, unknown).
expectType<string>('' as MyType<'foo'>); // Basic
expectType<any>('' as MyType<any>); // any/never/unknown
expectNotAssignable<MyType<'foo'>>({wrong: true}); // Negative
expectError<MyType<number>>(); // Should errorStudy: source/tuple-of.d.ts, is-equal.d.ts, literal-union.d.ts, internal/*.d.ts
Workflow
1. Research - Study source/ similar types + online research
2. Define - source/type-name.d.ts with docs
3. Test - test-d/type-name.ts with edge cases
4. Export - Add to index.d.ts + readme.md
5. Verify - npm test must pass
6. Commit - `Add TypeName type or TypeName: Fix description
Critical Rules
1. Import paths MUST include .d.ts extensionexport {};
2. Files MUST end with index.d.ts
3. Types MUST be exported from T
4. Type params MUST use descriptive names (not /U)any
5. MUST test , never, unknown edge cases//=>
6. First doc line MUST be concise (goes in readme)
7. Use realistic examples with comments@category
8. Include tags
9. Fix lint issues, don't disable rules
Type Programming
- Conditionals: T extends U ? X : Ytype Loop<T, Acc> = ... Loop<...> ...
- Recursion: infer Item
- Extract: T extends any ? ... : never
- Distribute: ['length']
- Count via tuple
- Union distribution is trickyβtest it
Philosophy
- Correctness > cleverness
- Real problems only
- Docs teach how, not what
- Edge cases mandatory
- One concept per PR
- Descriptive names > brevity
- No unrelated changes
Troubleshooting
- Tests fail? Check .d.ts imports, export {};, edge cases (any, never, unknown)index.d.ts`, fix issues (don't disable rules)
- Lint errors? Add to